* target.h: Add enum target_waitkind, enum target_signal, and
[deliverable/binutils-gdb.git] / gdb / hppa-tdep.c
CommitLineData
66a1aa07
SG
1/* Machine-dependent code which would otherwise be in inflow.c and core.c,
2 for GDB, the GNU debugger. This code is for the HP PA-RISC cpu.
3 Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4
5 Contributed by the Center for Software Science at the
6 University of Utah (pa-gdb-bugs@cs.utah.edu).
7
8This file is part of GDB.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23
24#include "defs.h"
25#include "frame.h"
26#include "inferior.h"
27#include "value.h"
28
29/* For argument passing to the inferior */
30#include "symtab.h"
31
32#ifdef USG
33#include <sys/types.h>
34#endif
35
36#include <sys/param.h>
37#include <sys/dir.h>
38#include <signal.h>
39#include <sys/ioctl.h>
40
41#ifdef COFF_ENCAPSULATE
42#include "a.out.encap.h"
43#else
44#include <a.out.h>
45#endif
46#ifndef N_SET_MAGIC
47#define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
48#endif
49
50/*#include <sys/user.h> After a.out.h */
51#include <sys/file.h>
52#include <sys/stat.h>
53#include <machine/psl.h>
54#include "wait.h"
55
56#include "gdbcore.h"
57#include "gdbcmd.h"
58#include "target.h"
59#include "symfile.h"
60#include "objfiles.h"
61
62static int restore_pc_queue PARAMS ((struct frame_saved_regs *fsr));
63static int hppa_alignof PARAMS ((struct type *arg));
8966221d
JK
64static FRAME_ADDR dig_fp_from_stack PARAMS ((FRAME frame,
65 struct unwind_table_entry *u));
8fa74880 66CORE_ADDR frame_saved_pc PARAMS ((FRAME frame));
66a1aa07
SG
67
68\f
69/* Routines to extract various sized constants out of hppa
70 instructions. */
71
72/* This assumes that no garbage lies outside of the lower bits of
73 value. */
74
75int
76sign_extend (val, bits)
77 unsigned val, bits;
78{
79 return (int)(val >> bits - 1 ? (-1 << bits) | val : val);
80}
81
82/* For many immediate values the sign bit is the low bit! */
83
84int
85low_sign_extend (val, bits)
86 unsigned val, bits;
87{
88 return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
89}
90/* extract the immediate field from a ld{bhw}s instruction */
91
92unsigned
93get_field (val, from, to)
94 unsigned val, from, to;
95{
96 val = val >> 31 - to;
97 return val & ((1 << 32 - from) - 1);
98}
99
100unsigned
101set_field (val, from, to, new_val)
102 unsigned *val, from, to;
103{
104 unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
105 return *val = *val & mask | (new_val << (31 - from));
106}
107
108/* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
109
110extract_3 (word)
111 unsigned word;
112{
113 return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
114}
115
116extract_5_load (word)
117 unsigned word;
118{
119 return low_sign_extend (word >> 16 & MASK_5, 5);
120}
121
122/* extract the immediate field from a st{bhw}s instruction */
123
124int
125extract_5_store (word)
126 unsigned word;
127{
128 return low_sign_extend (word & MASK_5, 5);
129}
130
68c8d698
SG
131/* extract the immediate field from a break instruction */
132
133unsigned
134extract_5r_store (word)
135 unsigned word;
136{
137 return (word & MASK_5);
138}
139
140/* extract the immediate field from a {sr}sm instruction */
141
142unsigned
143extract_5R_store (word)
144 unsigned word;
145{
146 return (word >> 16 & MASK_5);
147}
148
66a1aa07
SG
149/* extract an 11 bit immediate field */
150
151int
152extract_11 (word)
153 unsigned word;
154{
155 return low_sign_extend (word & MASK_11, 11);
156}
157
158/* extract a 14 bit immediate field */
159
160int
161extract_14 (word)
162 unsigned word;
163{
164 return low_sign_extend (word & MASK_14, 14);
165}
166
167/* deposit a 14 bit constant in a word */
168
169unsigned
170deposit_14 (opnd, word)
171 int opnd;
172 unsigned word;
173{
174 unsigned sign = (opnd < 0 ? 1 : 0);
175
176 return word | ((unsigned)opnd << 1 & MASK_14) | sign;
177}
178
179/* extract a 21 bit constant */
180
181int
182extract_21 (word)
183 unsigned word;
184{
185 int val;
186
187 word &= MASK_21;
188 word <<= 11;
189 val = GET_FIELD (word, 20, 20);
190 val <<= 11;
191 val |= GET_FIELD (word, 9, 19);
192 val <<= 2;
193 val |= GET_FIELD (word, 5, 6);
194 val <<= 5;
195 val |= GET_FIELD (word, 0, 4);
196 val <<= 2;
197 val |= GET_FIELD (word, 7, 8);
198 return sign_extend (val, 21) << 11;
199}
200
201/* deposit a 21 bit constant in a word. Although 21 bit constants are
202 usually the top 21 bits of a 32 bit constant, we assume that only
203 the low 21 bits of opnd are relevant */
204
205unsigned
206deposit_21 (opnd, word)
207 unsigned opnd, word;
208{
209 unsigned val = 0;
210
211 val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
212 val <<= 2;
213 val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
214 val <<= 2;
215 val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
216 val <<= 11;
217 val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
218 val <<= 1;
219 val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
220 return word | val;
221}
222
223/* extract a 12 bit constant from branch instructions */
224
225int
226extract_12 (word)
227 unsigned word;
228{
229 return sign_extend (GET_FIELD (word, 19, 28) |
230 GET_FIELD (word, 29, 29) << 10 |
231 (word & 0x1) << 11, 12) << 2;
232}
233
234/* extract a 17 bit constant from branch instructions, returning the
235 19 bit signed value. */
236
237int
238extract_17 (word)
239 unsigned word;
240{
241 return sign_extend (GET_FIELD (word, 19, 28) |
242 GET_FIELD (word, 29, 29) << 10 |
243 GET_FIELD (word, 11, 15) << 11 |
244 (word & 0x1) << 16, 17) << 2;
245}
246\f
66a1aa07
SG
247/* Lookup the unwind (stack backtrace) info for the given PC. We search all
248 of the objfiles seeking the unwind table entry for this PC. Each objfile
249 contains a sorted list of struct unwind_table_entry. Since we do a binary
250 search of the unwind tables, we depend upon them to be sorted. */
251
252static struct unwind_table_entry *
253find_unwind_entry(pc)
254 CORE_ADDR pc;
255{
256 int first, middle, last;
257 struct objfile *objfile;
258
259 ALL_OBJFILES (objfile)
260 {
261 struct obj_unwind_info *ui;
262
263 ui = OBJ_UNWIND_INFO (objfile);
264
265 if (!ui)
266 continue;
267
268 /* First, check the cache */
269
270 if (ui->cache
271 && pc >= ui->cache->region_start
272 && pc <= ui->cache->region_end)
273 return ui->cache;
274
275 /* Not in the cache, do a binary search */
276
277 first = 0;
278 last = ui->last;
279
280 while (first <= last)
281 {
282 middle = (first + last) / 2;
283 if (pc >= ui->table[middle].region_start
284 && pc <= ui->table[middle].region_end)
285 {
286 ui->cache = &ui->table[middle];
287 return &ui->table[middle];
288 }
289
290 if (pc < ui->table[middle].region_start)
291 last = middle - 1;
292 else
293 first = middle + 1;
294 }
295 } /* ALL_OBJFILES() */
296 return NULL;
297}
298
5ac7f56e
JK
299/* Called when no unwind descriptor was found for PC. Returns 1 if it
300 appears that PC is in a linker stub. */
301static int pc_in_linker_stub PARAMS ((CORE_ADDR));
302
303static int
304pc_in_linker_stub (pc)
305 CORE_ADDR pc;
306{
5ac7f56e
JK
307 int found_magic_instruction = 0;
308 int i;
08ecd8f3
JK
309 char buf[4];
310
311 /* If unable to read memory, assume pc is not in a linker stub. */
312 if (target_read_memory (pc, buf, 4) != 0)
313 return 0;
5ac7f56e 314
d08c6f4c
JK
315 /* We are looking for something like
316
317 ; $$dyncall jams RP into this special spot in the frame (RP')
318 ; before calling the "call stub"
319 ldw -18(sp),rp
320
321 ldsid (rp),r1 ; Get space associated with RP into r1
322 mtsp r1,sp ; Move it into space register 0
323 be,n 0(sr0),rp) ; back to your regularly scheduled program
324 */
325
5ac7f56e
JK
326 /* Maximum known linker stub size is 4 instructions. Search forward
327 from the given PC, then backward. */
328 for (i = 0; i < 4; i++)
329 {
6e35b037 330 /* If we hit something with an unwind, stop searching this direction. */
5ac7f56e
JK
331
332 if (find_unwind_entry (pc + i * 4) != 0)
333 break;
334
335 /* Check for ldsid (rp),r1 which is the magic instruction for a
336 return from a cross-space function call. */
337 if (read_memory_integer (pc + i * 4, 4) == 0x004010a1)
338 {
339 found_magic_instruction = 1;
340 break;
341 }
342 /* Add code to handle long call/branch and argument relocation stubs
343 here. */
344 }
345
346 if (found_magic_instruction != 0)
347 return 1;
348
349 /* Now look backward. */
350 for (i = 0; i < 4; i++)
351 {
6e35b037 352 /* If we hit something with an unwind, stop searching this direction. */
5ac7f56e
JK
353
354 if (find_unwind_entry (pc - i * 4) != 0)
355 break;
356
357 /* Check for ldsid (rp),r1 which is the magic instruction for a
358 return from a cross-space function call. */
359 if (read_memory_integer (pc - i * 4, 4) == 0x004010a1)
360 {
361 found_magic_instruction = 1;
362 break;
363 }
364 /* Add code to handle long call/branch and argument relocation stubs
365 here. */
366 }
367 return found_magic_instruction;
368}
369
66a1aa07
SG
370static int
371find_return_regnum(pc)
372 CORE_ADDR pc;
373{
374 struct unwind_table_entry *u;
375
376 u = find_unwind_entry (pc);
377
378 if (!u)
379 return RP_REGNUM;
380
381 if (u->Millicode)
382 return 31;
383
384 return RP_REGNUM;
385}
386
5ac7f56e 387/* Return size of frame, or -1 if we should use a frame pointer. */
66a1aa07
SG
388int
389find_proc_framesize(pc)
390 CORE_ADDR pc;
391{
392 struct unwind_table_entry *u;
393
66a1aa07
SG
394 u = find_unwind_entry (pc);
395
396 if (!u)
5ac7f56e
JK
397 {
398 if (pc_in_linker_stub (pc))
399 /* Linker stubs have a zero size frame. */
400 return 0;
401 else
402 return -1;
403 }
66a1aa07 404
eabbe766
JK
405 if (u->Save_SP)
406 /* If this bit is set, it means there is a frame pointer and we should
407 use it. */
408 return -1;
409
66a1aa07
SG
410 return u->Total_frame_size << 3;
411}
412
5ac7f56e
JK
413/* Return offset from sp at which rp is saved, or 0 if not saved. */
414static int rp_saved PARAMS ((CORE_ADDR));
415
416static int
417rp_saved (pc)
418 CORE_ADDR pc;
66a1aa07
SG
419{
420 struct unwind_table_entry *u;
421
422 u = find_unwind_entry (pc);
423
424 if (!u)
5ac7f56e
JK
425 {
426 if (pc_in_linker_stub (pc))
427 /* This is the so-called RP'. */
428 return -24;
429 else
430 return 0;
431 }
66a1aa07
SG
432
433 if (u->Save_RP)
5ac7f56e 434 return -20;
66a1aa07
SG
435 else
436 return 0;
437}
438\f
8fa74880
SG
439int
440frameless_function_invocation (frame)
441 FRAME frame;
442{
b8ec9a79 443 struct unwind_table_entry *u;
8fa74880 444
b8ec9a79 445 u = find_unwind_entry (frame->pc);
8fa74880 446
b8ec9a79 447 if (u == 0)
8fa74880 448 return frameless_look_for_prologue (frame);
b8ec9a79
JK
449
450 return (u->Total_frame_size == 0);
8fa74880
SG
451}
452
66a1aa07
SG
453CORE_ADDR
454saved_pc_after_call (frame)
455 FRAME frame;
456{
457 int ret_regnum;
458
459 ret_regnum = find_return_regnum (get_frame_pc (frame));
460
461 return read_register (ret_regnum) & ~0x3;
462}
463\f
464CORE_ADDR
465frame_saved_pc (frame)
466 FRAME frame;
467{
468 CORE_ADDR pc = get_frame_pc (frame);
469
8fa74880 470 if (frameless_function_invocation (frame))
66a1aa07
SG
471 {
472 int ret_regnum;
473
474 ret_regnum = find_return_regnum (pc);
475
476 return read_register (ret_regnum) & ~0x3;
477 }
66a1aa07 478 else
5ac7f56e
JK
479 {
480 int rp_offset = rp_saved (pc);
481
482 if (rp_offset == 0)
483 return read_register (RP_REGNUM) & ~0x3;
484 else
28403b8e 485 return read_memory_integer (frame->frame + rp_offset, 4) & ~0x3;
5ac7f56e 486 }
66a1aa07
SG
487}
488\f
489/* We need to correct the PC and the FP for the outermost frame when we are
490 in a system call. */
491
492void
493init_extra_frame_info (fromleaf, frame)
494 int fromleaf;
495 struct frame_info *frame;
496{
497 int flags;
498 int framesize;
499
192c3eeb 500 if (frame->next && !fromleaf)
66a1aa07
SG
501 return;
502
192c3eeb
JL
503 /* If the next frame represents a frameless function invocation
504 then we have to do some adjustments that are normally done by
505 FRAME_CHAIN. (FRAME_CHAIN is not called in this case.) */
506 if (fromleaf)
507 {
508 /* Find the framesize of *this* frame without peeking at the PC
509 in the current frame structure (it isn't set yet). */
510 framesize = find_proc_framesize (FRAME_SAVED_PC (get_next_frame (frame)));
511
512 /* Now adjust our base frame accordingly. If we have a frame pointer
513 use it, else subtract the size of this frame from the current
514 frame. (we always want frame->frame to point at the lowest address
515 in the frame). */
516 if (framesize == -1)
517 frame->frame = read_register (FP_REGNUM);
518 else
519 frame->frame -= framesize;
520 return;
521 }
522
66a1aa07
SG
523 flags = read_register (FLAGS_REGNUM);
524 if (flags & 2) /* In system call? */
525 frame->pc = read_register (31) & ~0x3;
526
192c3eeb
JL
527 /* The outermost frame is always derived from PC-framesize
528
529 One might think frameless innermost frames should have
530 a frame->frame that is the same as the parent's frame->frame.
531 That is wrong; frame->frame in that case should be the *high*
532 address of the parent's frame. It's complicated as hell to
533 explain, but the parent *always* creates some stack space for
534 the child. So the child actually does have a frame of some
535 sorts, and its base is the high address in its parent's frame. */
66a1aa07
SG
536 framesize = find_proc_framesize(frame->pc);
537 if (framesize == -1)
538 frame->frame = read_register (FP_REGNUM);
539 else
540 frame->frame = read_register (SP_REGNUM) - framesize;
66a1aa07
SG
541}
542\f
8966221d
JK
543/* Given a GDB frame, determine the address of the calling function's frame.
544 This will be used to create a new GDB frame struct, and then
545 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
546
547 This may involve searching through prologues for several functions
548 at boundaries where GCC calls HP C code, or where code which has
549 a frame pointer calls code without a frame pointer. */
550
551
66a1aa07
SG
552FRAME_ADDR
553frame_chain (frame)
554 struct frame_info *frame;
555{
8966221d
JK
556 int my_framesize, caller_framesize;
557 struct unwind_table_entry *u;
66a1aa07 558
8966221d
JK
559 /* Get frame sizes for the current frame and the frame of the
560 caller. */
561 my_framesize = find_proc_framesize (frame->pc);
562 caller_framesize = find_proc_framesize (FRAME_SAVED_PC(frame));
66a1aa07 563
8966221d
JK
564 /* If caller does not have a frame pointer, then its frame
565 can be found at current_frame - caller_framesize. */
566 if (caller_framesize != -1)
567 return frame->frame - caller_framesize;
568
569 /* Both caller and callee have frame pointers and are GCC compiled
570 (SAVE_SP bit in unwind descriptor is on for both functions.
571 The previous frame pointer is found at the top of the current frame. */
572 if (caller_framesize == -1 && my_framesize == -1)
573 return read_memory_integer (frame->frame, 4);
574
575 /* Caller has a frame pointer, but callee does not. This is a little
576 more difficult as GCC and HP C lay out locals and callee register save
577 areas very differently.
578
579 The previous frame pointer could be in a register, or in one of
580 several areas on the stack.
581
582 Walk from the current frame to the innermost frame examining
2f8c3639 583 unwind descriptors to determine if %r3 ever gets saved into the
8966221d 584 stack. If so return whatever value got saved into the stack.
2f8c3639 585 If it was never saved in the stack, then the value in %r3 is still
8966221d
JK
586 valid, so use it.
587
2f8c3639 588 We use information from unwind descriptors to determine if %r3
8966221d
JK
589 is saved into the stack (Entry_GR field has this information). */
590
591 while (frame)
592 {
593 u = find_unwind_entry (frame->pc);
594
595 if (!u)
596 {
01a03545
JK
597 /* We could find this information by examining prologues. I don't
598 think anyone has actually written any tools (not even "strip")
599 which leave them out of an executable, so maybe this is a moot
600 point. */
8966221d
JK
601 warning ("Unable to find unwind for PC 0x%x -- Help!", frame->pc);
602 return 0;
603 }
604
605 /* Entry_GR specifies the number of callee-saved general registers
2f8c3639
JL
606 saved in the stack. It starts at %r3, so %r3 would be 1. */
607 if (u->Entry_GR >= 1 || u->Save_SP)
8966221d
JK
608 break;
609 else
610 frame = frame->next;
611 }
612
613 if (frame)
614 {
615 /* We may have walked down the chain into a function with a frame
616 pointer. */
617 if (u->Save_SP)
618 return read_memory_integer (frame->frame, 4);
2f8c3639 619 /* %r3 was saved somewhere in the stack. Dig it out. */
8966221d
JK
620 else
621 return dig_fp_from_stack (frame, u);
622 }
623 else
624 {
2f8c3639 625 /* The value in %r3 was never saved into the stack (thus %r3 still
8966221d 626 holds the value of the previous frame pointer). */
2f8c3639 627 return read_register (FP_REGNUM);
8966221d
JK
628 }
629}
66a1aa07 630
8966221d
JK
631/* Given a frame and an unwind descriptor return the value for %fr (aka fp)
632 which was saved into the stack. FIXME: Why can't we just use the standard
633 saved_regs stuff? */
634
635static FRAME_ADDR
636dig_fp_from_stack (frame, u)
637 FRAME frame;
638 struct unwind_table_entry *u;
639{
640 CORE_ADDR pc = u->region_start;
641
2f8c3639 642 /* Search the function for the save of %r3. */
8966221d
JK
643 while (pc != u->region_end)
644 {
645 char buf[4];
646 unsigned long inst;
647 int status;
648
2f8c3639 649 /* We need only look for the standard stw %r3,X(%sp) instruction,
8966221d
JK
650 the other variants (eg stwm) are only used on the first register
651 save (eg %r3). */
652 status = target_read_memory (pc, buf, 4);
653 inst = extract_unsigned_integer (buf, 4);
654
655 if (status != 0)
656 memory_error (status, pc);
657
2f8c3639
JL
658 /* Check for stw %r3,X(%sp). */
659 if ((inst & 0xffffc000) == 0x6bc30000)
8966221d 660 {
2f8c3639 661 /* Found the instruction which saves %r3. The offset (relative
8966221d
JK
662 to this frame) is framesize + immed14 (derived from the
663 store instruction). */
664 int offset = (u->Total_frame_size << 3) + extract_14 (inst);
665
666 return read_memory_integer (frame->frame + offset, 4);
667 }
668
669 /* Keep looking. */
670 pc += 4;
671 }
672
2f8c3639 673 warning ("Unable to find %%r3 in stack.\n");
8966221d 674 return 0;
66a1aa07 675}
8966221d 676
66a1aa07
SG
677\f
678/* To see if a frame chain is valid, see if the caller looks like it
679 was compiled with gcc. */
680
681int
682frame_chain_valid (chain, thisframe)
683 FRAME_ADDR chain;
684 FRAME thisframe;
685{
247145e6
JK
686 struct minimal_symbol *msym_us;
687 struct minimal_symbol *msym_start;
4432b9f9 688 struct unwind_table_entry *u;
66a1aa07
SG
689
690 if (!chain)
691 return 0;
692
b8ec9a79 693 u = find_unwind_entry (thisframe->pc);
4b01383b 694
247145e6
JK
695 /* We can't just check that the same of msym_us is "_start", because
696 someone idiotically decided that they were going to make a Ltext_end
697 symbol with the same address. This Ltext_end symbol is totally
698 indistinguishable (as nearly as I can tell) from the symbol for a function
699 which is (legitimately, since it is in the user's namespace)
700 named Ltext_end, so we can't just ignore it. */
701 msym_us = lookup_minimal_symbol_by_pc (FRAME_SAVED_PC (thisframe));
702 msym_start = lookup_minimal_symbol ("_start", NULL);
703 if (msym_us
704 && msym_start
705 && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
b8ec9a79 706 return 0;
5ac7f56e 707
b8ec9a79
JK
708 if (u == NULL)
709 return 1;
5ac7f56e 710
b8ec9a79
JK
711 if (u->Save_SP || u->Total_frame_size)
712 return 1;
5ac7f56e 713
b8ec9a79
JK
714 if (pc_in_linker_stub (thisframe->pc))
715 return 1;
4b01383b 716
b8ec9a79 717 return 0;
66a1aa07
SG
718}
719
66a1aa07
SG
720/*
721 * These functions deal with saving and restoring register state
722 * around a function call in the inferior. They keep the stack
723 * double-word aligned; eventually, on an hp700, the stack will have
724 * to be aligned to a 64-byte boundary.
725 */
726
727int
728push_dummy_frame ()
729{
730 register CORE_ADDR sp;
731 register int regnum;
732 int int_buffer;
733 double freg_buffer;
734
735 /* Space for "arguments"; the RP goes in here. */
736 sp = read_register (SP_REGNUM) + 48;
737 int_buffer = read_register (RP_REGNUM) | 0x3;
738 write_memory (sp - 20, (char *)&int_buffer, 4);
739
740 int_buffer = read_register (FP_REGNUM);
741 write_memory (sp, (char *)&int_buffer, 4);
742
743 write_register (FP_REGNUM, sp);
744
745 sp += 8;
746
747 for (regnum = 1; regnum < 32; regnum++)
748 if (regnum != RP_REGNUM && regnum != FP_REGNUM)
749 sp = push_word (sp, read_register (regnum));
750
751 sp += 4;
752
753 for (regnum = FP0_REGNUM; regnum < NUM_REGS; regnum++)
754 {
755 read_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
756 sp = push_bytes (sp, (char *)&freg_buffer, 8);
757 }
758 sp = push_word (sp, read_register (IPSW_REGNUM));
759 sp = push_word (sp, read_register (SAR_REGNUM));
760 sp = push_word (sp, read_register (PCOQ_HEAD_REGNUM));
761 sp = push_word (sp, read_register (PCSQ_HEAD_REGNUM));
762 sp = push_word (sp, read_register (PCOQ_TAIL_REGNUM));
763 sp = push_word (sp, read_register (PCSQ_TAIL_REGNUM));
764 write_register (SP_REGNUM, sp);
765}
766
767find_dummy_frame_regs (frame, frame_saved_regs)
768 struct frame_info *frame;
769 struct frame_saved_regs *frame_saved_regs;
770{
771 CORE_ADDR fp = frame->frame;
772 int i;
773
774 frame_saved_regs->regs[RP_REGNUM] = fp - 20 & ~0x3;
775 frame_saved_regs->regs[FP_REGNUM] = fp;
776 frame_saved_regs->regs[1] = fp + 8;
66a1aa07 777
b227992a
SG
778 for (fp += 12, i = 3; i < 32; i++)
779 {
780 if (i != FP_REGNUM)
781 {
782 frame_saved_regs->regs[i] = fp;
783 fp += 4;
784 }
785 }
66a1aa07
SG
786
787 fp += 4;
788 for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
789 frame_saved_regs->regs[i] = fp;
790
791 frame_saved_regs->regs[IPSW_REGNUM] = fp;
b227992a
SG
792 frame_saved_regs->regs[SAR_REGNUM] = fp + 4;
793 frame_saved_regs->regs[PCOQ_HEAD_REGNUM] = fp + 8;
794 frame_saved_regs->regs[PCSQ_HEAD_REGNUM] = fp + 12;
795 frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp + 16;
796 frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp + 20;
66a1aa07
SG
797}
798
799int
800hppa_pop_frame ()
801{
802 register FRAME frame = get_current_frame ();
803 register CORE_ADDR fp;
804 register int regnum;
805 struct frame_saved_regs fsr;
806 struct frame_info *fi;
807 double freg_buffer;
808
809 fi = get_frame_info (frame);
810 fp = fi->frame;
811 get_frame_saved_regs (fi, &fsr);
812
813 if (fsr.regs[IPSW_REGNUM]) /* Restoring a call dummy frame */
814 restore_pc_queue (&fsr);
815
816 for (regnum = 31; regnum > 0; regnum--)
817 if (fsr.regs[regnum])
818 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
819
820 for (regnum = NUM_REGS - 1; regnum >= FP0_REGNUM ; regnum--)
821 if (fsr.regs[regnum])
822 {
823 read_memory (fsr.regs[regnum], (char *)&freg_buffer, 8);
824 write_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
825 }
826
827 if (fsr.regs[IPSW_REGNUM])
828 write_register (IPSW_REGNUM,
829 read_memory_integer (fsr.regs[IPSW_REGNUM], 4));
830
831 if (fsr.regs[SAR_REGNUM])
832 write_register (SAR_REGNUM,
833 read_memory_integer (fsr.regs[SAR_REGNUM], 4));
834
ed1a07ad 835 /* If the PC was explicitly saved, then just restore it. */
66a1aa07
SG
836 if (fsr.regs[PCOQ_TAIL_REGNUM])
837 write_register (PCOQ_TAIL_REGNUM,
838 read_memory_integer (fsr.regs[PCOQ_TAIL_REGNUM], 4));
839
ed1a07ad
JK
840 /* Else use the value in %rp to set the new PC. */
841 else
842 target_write_pc (read_register (RP_REGNUM));
843
66a1aa07
SG
844 write_register (FP_REGNUM, read_memory_integer (fp, 4));
845
846 if (fsr.regs[IPSW_REGNUM]) /* call dummy */
847 write_register (SP_REGNUM, fp - 48);
848 else
849 write_register (SP_REGNUM, fp);
850
851 flush_cached_frames ();
852 set_current_frame (create_new_frame (read_register (FP_REGNUM),
853 read_pc ()));
854}
855
856/*
857 * After returning to a dummy on the stack, restore the instruction
858 * queue space registers. */
859
860static int
861restore_pc_queue (fsr)
862 struct frame_saved_regs *fsr;
863{
864 CORE_ADDR pc = read_pc ();
865 CORE_ADDR new_pc = read_memory_integer (fsr->regs[PCOQ_HEAD_REGNUM], 4);
866 int pid;
67ac9759 867 struct target_waitstatus w;
66a1aa07
SG
868 int insn_count;
869
870 /* Advance past break instruction in the call dummy. */
871 write_register (PCOQ_HEAD_REGNUM, pc + 4);
872 write_register (PCOQ_TAIL_REGNUM, pc + 8);
873
874 /*
875 * HPUX doesn't let us set the space registers or the space
876 * registers of the PC queue through ptrace. Boo, hiss.
877 * Conveniently, the call dummy has this sequence of instructions
878 * after the break:
879 * mtsp r21, sr0
880 * ble,n 0(sr0, r22)
881 *
882 * So, load up the registers and single step until we are in the
883 * right place.
884 */
885
886 write_register (21, read_memory_integer (fsr->regs[PCSQ_HEAD_REGNUM], 4));
887 write_register (22, new_pc);
888
889 for (insn_count = 0; insn_count < 3; insn_count++)
890 {
8c5e0021
JK
891 /* FIXME: What if the inferior gets a signal right now? Want to
892 merge this into wait_for_inferior (as a special kind of
893 watchpoint? By setting a breakpoint at the end? Is there
894 any other choice? Is there *any* way to do this stuff with
895 ptrace() or some equivalent?). */
66a1aa07 896 resume (1, 0);
67ac9759 897 target_wait (inferior_pid, &w);
66a1aa07 898
67ac9759 899 if (w.kind == TARGET_WAITKIND_SIGNALLED)
66a1aa07 900 {
67ac9759 901 stop_signal = w.value.sig;
66a1aa07 902 terminal_ours_for_output ();
67ac9759
JK
903 printf_unfiltered ("\nProgram terminated with signal %s, %s.\n",
904 target_signal_to_name (stop_signal),
905 target_signal_to_string (stop_signal));
199b2450 906 gdb_flush (gdb_stdout);
66a1aa07
SG
907 return 0;
908 }
909 }
8c5e0021 910 target_terminal_ours ();
66a1aa07
SG
911 fetch_inferior_registers (-1);
912 return 1;
913}
914
915CORE_ADDR
916hppa_push_arguments (nargs, args, sp, struct_return, struct_addr)
917 int nargs;
918 value *args;
919 CORE_ADDR sp;
920 int struct_return;
921 CORE_ADDR struct_addr;
922{
923 /* array of arguments' offsets */
1edc5cd2 924 int *offset = (int *)alloca(nargs * sizeof (int));
66a1aa07
SG
925 int cum = 0;
926 int i, alignment;
927
928 for (i = 0; i < nargs; i++)
929 {
930 /* Coerce chars to int & float to double if necessary */
931 args[i] = value_arg_coerce (args[i]);
932
933 cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
934
935 /* value must go at proper alignment. Assume alignment is a
936 power of two.*/
937 alignment = hppa_alignof (VALUE_TYPE (args[i]));
938 if (cum % alignment)
939 cum = (cum + alignment) & -alignment;
940 offset[i] = -cum;
941 }
558f4183 942 sp += max ((cum + 7) & -8, 16);
66a1aa07
SG
943
944 for (i = 0; i < nargs; i++)
945 write_memory (sp + offset[i], VALUE_CONTENTS (args[i]),
946 TYPE_LENGTH (VALUE_TYPE (args[i])));
947
948 if (struct_return)
949 write_register (28, struct_addr);
950 return sp + 32;
951}
952
953/*
954 * Insert the specified number of args and function address
955 * into a call sequence of the above form stored at DUMMYNAME.
956 *
957 * On the hppa we need to call the stack dummy through $$dyncall.
958 * Therefore our version of FIX_CALL_DUMMY takes an extra argument,
959 * real_pc, which is the location where gdb should start up the
960 * inferior to do the function call.
961 */
962
963CORE_ADDR
964hppa_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
f4f0d174 965 char *dummy;
66a1aa07
SG
966 CORE_ADDR pc;
967 CORE_ADDR fun;
968 int nargs;
969 value *args;
970 struct type *type;
971 int gcc_p;
972{
973 CORE_ADDR dyncall_addr, sr4export_addr;
974 struct minimal_symbol *msymbol;
6cfec929 975 int flags = read_register (FLAGS_REGNUM);
66a1aa07
SG
976
977 msymbol = lookup_minimal_symbol ("$$dyncall", (struct objfile *) NULL);
978 if (msymbol == NULL)
979 error ("Can't find an address for $$dyncall trampoline");
980
981 dyncall_addr = SYMBOL_VALUE_ADDRESS (msymbol);
982
983 msymbol = lookup_minimal_symbol ("_sr4export", (struct objfile *) NULL);
984 if (msymbol == NULL)
985 error ("Can't find an address for _sr4export trampoline");
986
987 sr4export_addr = SYMBOL_VALUE_ADDRESS (msymbol);
988
f4f0d174
JK
989 store_unsigned_integer
990 (&dummy[9*REGISTER_SIZE],
991 REGISTER_SIZE,
992 deposit_21 (fun >> 11,
993 extract_unsigned_integer (&dummy[9*REGISTER_SIZE],
994 REGISTER_SIZE)));
995 store_unsigned_integer
996 (&dummy[10*REGISTER_SIZE],
997 REGISTER_SIZE,
998 deposit_14 (fun & MASK_11,
999 extract_unsigned_integer (&dummy[10*REGISTER_SIZE],
1000 REGISTER_SIZE)));
1001 store_unsigned_integer
1002 (&dummy[12*REGISTER_SIZE],
1003 REGISTER_SIZE,
1004 deposit_21 (sr4export_addr >> 11,
1005 extract_unsigned_integer (&dummy[12*REGISTER_SIZE],
1006 REGISTER_SIZE)));
1007 store_unsigned_integer
1008 (&dummy[13*REGISTER_SIZE],
1009 REGISTER_SIZE,
1010 deposit_14 (sr4export_addr & MASK_11,
1011 extract_unsigned_integer (&dummy[13*REGISTER_SIZE],
1012 REGISTER_SIZE)));
66a1aa07
SG
1013
1014 write_register (22, pc);
1015
6cfec929
JK
1016 /* If we are in a syscall, then we should call the stack dummy
1017 directly. $$dyncall is not needed as the kernel sets up the
1018 space id registers properly based on the value in %r31. In
1019 fact calling $$dyncall will not work because the value in %r22
1020 will be clobbered on the syscall exit path. */
1021 if (flags & 2)
1022 return pc;
1023 else
1024 return dyncall_addr;
1025
66a1aa07
SG
1026}
1027
d3862cae
JK
1028/* Get the PC from %r31 if currently in a syscall. Also mask out privilege
1029 bits. */
1030CORE_ADDR
1031target_read_pc ()
1032{
1033 int flags = read_register (FLAGS_REGNUM);
1034
1035 if (flags & 2)
1036 return read_register (31) & ~0x3;
1037 return read_register (PC_REGNUM) & ~0x3;
1038}
1039
6cfec929
JK
1040/* Write out the PC. If currently in a syscall, then also write the new
1041 PC value into %r31. */
1042void
1043target_write_pc (v)
1044 CORE_ADDR v;
1045{
1046 int flags = read_register (FLAGS_REGNUM);
1047
1048 /* If in a syscall, then set %r31. Also make sure to get the
1049 privilege bits set correctly. */
1050 if (flags & 2)
1051 write_register (31, (long) (v | 0x3));
1052
1053 write_register (PC_REGNUM, (long) v);
1054 write_register (NPC_REGNUM, (long) v + 4);
1055}
1056
66a1aa07
SG
1057/* return the alignment of a type in bytes. Structures have the maximum
1058 alignment required by their fields. */
1059
1060static int
1061hppa_alignof (arg)
1062 struct type *arg;
1063{
1064 int max_align, align, i;
1065 switch (TYPE_CODE (arg))
1066 {
1067 case TYPE_CODE_PTR:
1068 case TYPE_CODE_INT:
1069 case TYPE_CODE_FLT:
1070 return TYPE_LENGTH (arg);
1071 case TYPE_CODE_ARRAY:
1072 return hppa_alignof (TYPE_FIELD_TYPE (arg, 0));
1073 case TYPE_CODE_STRUCT:
1074 case TYPE_CODE_UNION:
1075 max_align = 2;
1076 for (i = 0; i < TYPE_NFIELDS (arg); i++)
1077 {
1078 /* Bit fields have no real alignment. */
1079 if (!TYPE_FIELD_BITPOS (arg, i))
1080 {
1081 align = hppa_alignof (TYPE_FIELD_TYPE (arg, i));
1082 max_align = max (max_align, align);
1083 }
1084 }
1085 return max_align;
1086 default:
1087 return 4;
1088 }
1089}
1090
1091/* Print the register regnum, or all registers if regnum is -1 */
1092
1093pa_do_registers_info (regnum, fpregs)
1094 int regnum;
1095 int fpregs;
1096{
1097 char raw_regs [REGISTER_BYTES];
1098 int i;
1099
1100 for (i = 0; i < NUM_REGS; i++)
1101 read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
1102 if (regnum == -1)
1103 pa_print_registers (raw_regs, regnum, fpregs);
1104 else if (regnum < FP0_REGNUM)
199b2450 1105 printf_unfiltered ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
66a1aa07
SG
1106 REGISTER_BYTE (regnum)));
1107 else
1108 pa_print_fp_reg (regnum);
1109}
1110
1111pa_print_registers (raw_regs, regnum, fpregs)
1112 char *raw_regs;
1113 int regnum;
1114 int fpregs;
1115{
1116 int i;
1117
1118 for (i = 0; i < 18; i++)
199b2450 1119 printf_unfiltered ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
66a1aa07
SG
1120 reg_names[i],
1121 *(int *)(raw_regs + REGISTER_BYTE (i)),
1122 reg_names[i + 18],
1123 *(int *)(raw_regs + REGISTER_BYTE (i + 18)),
1124 reg_names[i + 36],
1125 *(int *)(raw_regs + REGISTER_BYTE (i + 36)),
1126 reg_names[i + 54],
1127 *(int *)(raw_regs + REGISTER_BYTE (i + 54)));
1128
1129 if (fpregs)
1130 for (i = 72; i < NUM_REGS; i++)
1131 pa_print_fp_reg (i);
1132}
1133
1134pa_print_fp_reg (i)
1135 int i;
1136{
1137 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
1138 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
66a1aa07 1139
ad09cb2b 1140 /* Get the data in raw format. */
66a1aa07 1141 read_relative_register_raw_bytes (i, raw_buffer);
ad09cb2b
PS
1142
1143 /* Convert raw data to virtual format if necessary. */
1144#ifdef REGISTER_CONVERTIBLE
1145 if (REGISTER_CONVERTIBLE (i))
1146 {
1147 REGISTER_CONVERT_TO_VIRTUAL (i, REGISTER_VIRTUAL_TYPE (i),
1148 raw_buffer, virtual_buffer);
1149 }
1150 else
1151#endif
1152 memcpy (virtual_buffer, raw_buffer,
1153 REGISTER_VIRTUAL_SIZE (i));
66a1aa07 1154
199b2450
TL
1155 fputs_filtered (reg_names[i], gdb_stdout);
1156 print_spaces_filtered (15 - strlen (reg_names[i]), gdb_stdout);
66a1aa07 1157
199b2450 1158 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, gdb_stdout, 0,
66a1aa07
SG
1159 1, 0, Val_pretty_default);
1160 printf_filtered ("\n");
1161}
1162
1163/* Function calls that pass into a new compilation unit must pass through a
1164 small piece of code that does long format (`external' in HPPA parlance)
1165 jumps. We figure out where the trampoline is going to end up, and return
1166 the PC of the final destination. If we aren't in a trampoline, we just
1167 return NULL.
1168
1169 For computed calls, we just extract the new PC from r22. */
1170
1171CORE_ADDR
1172skip_trampoline_code (pc, name)
1173 CORE_ADDR pc;
1174 char *name;
1175{
1176 long inst0, inst1;
1177 static CORE_ADDR dyncall = 0;
1178 struct minimal_symbol *msym;
1179
1180/* FIXME XXX - dyncall must be initialized whenever we get a new exec file */
1181
1182 if (!dyncall)
1183 {
1184 msym = lookup_minimal_symbol ("$$dyncall", NULL);
1185 if (msym)
1186 dyncall = SYMBOL_VALUE_ADDRESS (msym);
1187 else
1188 dyncall = -1;
1189 }
1190
1191 if (pc == dyncall)
1192 return (CORE_ADDR)(read_register (22) & ~0x3);
1193
1194 inst0 = read_memory_integer (pc, 4);
1195 inst1 = read_memory_integer (pc+4, 4);
1196
1197 if ( (inst0 & 0xffe00000) == 0x20200000 /* ldil xxx, r1 */
1198 && (inst1 & 0xffe0e002) == 0xe0202002) /* be,n yyy(sr4, r1) */
1199 pc = extract_21 (inst0) + extract_17 (inst1);
1200 else
1201 pc = (CORE_ADDR)NULL;
1202
1203 return pc;
1204}
1205
1206/* Advance PC across any function entry prologue instructions
1207 to reach some "real" code. */
1208
1209/* skip (stw rp, -20(0,sp)); copy 4,1; copy sp, 4; stwm 1,framesize(sp)
1210 for gcc, or (stw rp, -20(0,sp); stwm 1, framesize(sp) for hcc */
1211
1212CORE_ADDR
1213skip_prologue(pc)
1214 CORE_ADDR pc;
1215{
34df79fc
JK
1216 char buf[4];
1217 unsigned long inst;
66a1aa07
SG
1218 int status;
1219
34df79fc
JK
1220 status = target_read_memory (pc, buf, 4);
1221 inst = extract_unsigned_integer (buf, 4);
66a1aa07
SG
1222 if (status != 0)
1223 return pc;
1224
1225 if (inst == 0x6BC23FD9) /* stw rp,-20(sp) */
1226 {
2f8c3639 1227 if (read_memory_integer (pc + 4, 4) == 0x8030241) /* copy r3,r1 */
66a1aa07 1228 pc += 16;
2f8c3639 1229 else if ((read_memory_integer (pc + 4, 4) & ~MASK_14) == 0x68710000) /* stw r1,(r3) */
66a1aa07
SG
1230 pc += 8;
1231 }
2f8c3639 1232 else if (read_memory_integer (pc, 4) == 0x8030241) /* copy r3,r1 */
66a1aa07 1233 pc += 12;
2f8c3639 1234 else if ((read_memory_integer (pc, 4) & ~MASK_14) == 0x68710000) /* stw r1,(r3) */
66a1aa07
SG
1235 pc += 4;
1236
1237 return pc;
1238}
1239
63757ecd
JK
1240#ifdef MAINTENANCE_CMDS
1241
66a1aa07
SG
1242static void
1243unwind_command (exp, from_tty)
1244 char *exp;
1245 int from_tty;
1246{
1247 CORE_ADDR address;
1248 union
1249 {
1250 int *foo;
1251 struct unwind_table_entry *u;
1252 } xxx;
1253
1254 /* If we have an expression, evaluate it and use it as the address. */
1255
1256 if (exp != 0 && *exp != 0)
1257 address = parse_and_eval_address (exp);
1258 else
1259 return;
1260
1261 xxx.u = find_unwind_entry (address);
1262
1263 if (!xxx.u)
1264 {
199b2450 1265 printf_unfiltered ("Can't find unwind table entry for PC 0x%x\n", address);
66a1aa07
SG
1266 return;
1267 }
1268
199b2450 1269 printf_unfiltered ("%08x\n%08X\n%08X\n%08X\n", xxx.foo[0], xxx.foo[1], xxx.foo[2],
66a1aa07
SG
1270 xxx.foo[3]);
1271}
976bb0be 1272#endif /* MAINTENANCE_CMDS */
63757ecd
JK
1273
1274void
1275_initialize_hppa_tdep ()
1276{
976bb0be 1277#ifdef MAINTENANCE_CMDS
63757ecd
JK
1278 add_cmd ("unwind", class_maintenance, unwind_command,
1279 "Print unwind table entry at given address.",
1280 &maintenanceprintlist);
63757ecd 1281#endif /* MAINTENANCE_CMDS */
976bb0be 1282}
This page took 0.121219 seconds and 4 git commands to generate.