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.
5 Contributed by the Center for Software Science at the
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* For argument passing to the inferior */
33 #include <sys/types.h>
36 #include <sys/param.h>
39 #include <sys/ioctl.h>
41 #ifdef COFF_ENCAPSULATE
42 #include "a.out.encap.h"
47 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
50 /*#include <sys/user.h> After a.out.h */
53 #include <machine/psl.h>
62 static int restore_pc_queue PARAMS ((struct frame_saved_regs *fsr));
63 static int hppa_alignof PARAMS ((struct type *arg));
66 /* Routines to extract various sized constants out of hppa
69 /* This assumes that no garbage lies outside of the lower bits of
73 sign_extend (val, bits)
76 return (int)(val >> bits - 1 ? (-1 << bits) | val : val);
79 /* For many immediate values the sign bit is the low bit! */
82 low_sign_extend (val, bits)
85 return (int)((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
87 /* extract the immediate field from a ld{bhw}s instruction */
90 get_field (val, from, to)
91 unsigned val, from, to;
94 return val & ((1 << 32 - from) - 1);
98 set_field (val, from, to, new_val)
99 unsigned *val, from, to;
101 unsigned mask = ~((1 << (to - from + 1)) << (31 - from));
102 return *val = *val & mask | (new_val << (31 - from));
105 /* extract a 3-bit space register number from a be, ble, mtsp or mfsp */
110 return GET_FIELD (word, 18, 18) << 2 | GET_FIELD (word, 16, 17);
113 extract_5_load (word)
116 return low_sign_extend (word >> 16 & MASK_5, 5);
119 /* extract the immediate field from a st{bhw}s instruction */
122 extract_5_store (word)
125 return low_sign_extend (word & MASK_5, 5);
128 /* extract the immediate field from a break instruction */
131 extract_5r_store (word)
134 return (word & MASK_5);
137 /* extract the immediate field from a {sr}sm instruction */
140 extract_5R_store (word)
143 return (word >> 16 & MASK_5);
146 /* extract an 11 bit immediate field */
152 return low_sign_extend (word & MASK_11, 11);
155 /* extract a 14 bit immediate field */
161 return low_sign_extend (word & MASK_14, 14);
164 /* deposit a 14 bit constant in a word */
167 deposit_14 (opnd, word)
171 unsigned sign = (opnd < 0 ? 1 : 0);
173 return word | ((unsigned)opnd << 1 & MASK_14) | sign;
176 /* extract a 21 bit constant */
186 val = GET_FIELD (word, 20, 20);
188 val |= GET_FIELD (word, 9, 19);
190 val |= GET_FIELD (word, 5, 6);
192 val |= GET_FIELD (word, 0, 4);
194 val |= GET_FIELD (word, 7, 8);
195 return sign_extend (val, 21) << 11;
198 /* deposit a 21 bit constant in a word. Although 21 bit constants are
199 usually the top 21 bits of a 32 bit constant, we assume that only
200 the low 21 bits of opnd are relevant */
203 deposit_21 (opnd, word)
208 val |= GET_FIELD (opnd, 11 + 14, 11 + 18);
210 val |= GET_FIELD (opnd, 11 + 12, 11 + 13);
212 val |= GET_FIELD (opnd, 11 + 19, 11 + 20);
214 val |= GET_FIELD (opnd, 11 + 1, 11 + 11);
216 val |= GET_FIELD (opnd, 11 + 0, 11 + 0);
220 /* extract a 12 bit constant from branch instructions */
226 return sign_extend (GET_FIELD (word, 19, 28) |
227 GET_FIELD (word, 29, 29) << 10 |
228 (word & 0x1) << 11, 12) << 2;
231 /* extract a 17 bit constant from branch instructions, returning the
232 19 bit signed value. */
238 return sign_extend (GET_FIELD (word, 19, 28) |
239 GET_FIELD (word, 29, 29) << 10 |
240 GET_FIELD (word, 11, 15) << 11 |
241 (word & 0x1) << 16, 17) << 2;
244 static int use_unwind = 0;
246 /* Lookup the unwind (stack backtrace) info for the given PC. We search all
247 of the objfiles seeking the unwind table entry for this PC. Each objfile
248 contains a sorted list of struct unwind_table_entry. Since we do a binary
249 search of the unwind tables, we depend upon them to be sorted. */
251 static struct unwind_table_entry *
252 find_unwind_entry(pc)
255 int first, middle, last;
256 struct objfile *objfile;
258 ALL_OBJFILES (objfile)
260 struct obj_unwind_info *ui;
262 ui = OBJ_UNWIND_INFO (objfile);
267 /* First, check the cache */
270 && pc >= ui->cache->region_start
271 && pc <= ui->cache->region_end)
274 /* Not in the cache, do a binary search */
279 while (first <= last)
281 middle = (first + last) / 2;
282 if (pc >= ui->table[middle].region_start
283 && pc <= ui->table[middle].region_end)
285 ui->cache = &ui->table[middle];
286 return &ui->table[middle];
289 if (pc < ui->table[middle].region_start)
294 } /* ALL_OBJFILES() */
299 find_return_regnum(pc)
302 struct unwind_table_entry *u;
304 u = find_unwind_entry (pc);
316 find_proc_framesize(pc)
319 struct unwind_table_entry *u;
324 u = find_unwind_entry (pc);
329 return u->Total_frame_size << 3;
335 struct unwind_table_entry *u;
337 u = find_unwind_entry (pc);
349 saved_pc_after_call (frame)
354 ret_regnum = find_return_regnum (get_frame_pc (frame));
356 return read_register (ret_regnum) & ~0x3;
360 frame_saved_pc (frame)
363 CORE_ADDR pc = get_frame_pc (frame);
365 if (frameless_look_for_prologue (frame))
369 ret_regnum = find_return_regnum (pc);
371 return read_register (ret_regnum) & ~0x3;
373 else if (rp_saved (pc))
374 return read_memory_integer (frame->frame - 20, 4) & ~0x3;
376 return read_register (RP_REGNUM) & ~0x3;
379 /* We need to correct the PC and the FP for the outermost frame when we are
383 init_extra_frame_info (fromleaf, frame)
385 struct frame_info *frame;
390 if (frame->next) /* Only do this for outermost frame */
393 flags = read_register (FLAGS_REGNUM);
394 if (flags & 2) /* In system call? */
395 frame->pc = read_register (31) & ~0x3;
397 /* The outermost frame is always derived from PC-framesize */
398 framesize = find_proc_framesize(frame->pc);
400 frame->frame = read_register (FP_REGNUM);
402 frame->frame = read_register (SP_REGNUM) - framesize;
404 if (!frameless_look_for_prologue (frame)) /* Frameless? */
405 return; /* No, quit now */
407 /* For frameless functions, we need to look at the caller's frame */
408 framesize = find_proc_framesize(FRAME_SAVED_PC(frame));
410 frame->frame -= framesize;
415 struct frame_info *frame;
419 framesize = find_proc_framesize(FRAME_SAVED_PC(frame));
422 return frame->frame - framesize;
424 return read_memory_integer (frame->frame, 4);
427 /* To see if a frame chain is valid, see if the caller looks like it
428 was compiled with gcc. */
431 frame_chain_valid (chain, thisframe)
435 struct minimal_symbol *msym;
440 msym = lookup_minimal_symbol_by_pc (FRAME_SAVED_PC (thisframe));
443 && (strcmp (SYMBOL_NAME (msym), "_start") == 0))
450 * These functions deal with saving and restoring register state
451 * around a function call in the inferior. They keep the stack
452 * double-word aligned; eventually, on an hp700, the stack will have
453 * to be aligned to a 64-byte boundary.
459 register CORE_ADDR sp;
464 /* Space for "arguments"; the RP goes in here. */
465 sp = read_register (SP_REGNUM) + 48;
466 int_buffer = read_register (RP_REGNUM) | 0x3;
467 write_memory (sp - 20, (char *)&int_buffer, 4);
469 int_buffer = read_register (FP_REGNUM);
470 write_memory (sp, (char *)&int_buffer, 4);
472 write_register (FP_REGNUM, sp);
476 for (regnum = 1; regnum < 32; regnum++)
477 if (regnum != RP_REGNUM && regnum != FP_REGNUM)
478 sp = push_word (sp, read_register (regnum));
482 for (regnum = FP0_REGNUM; regnum < NUM_REGS; regnum++)
484 read_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
485 sp = push_bytes (sp, (char *)&freg_buffer, 8);
487 sp = push_word (sp, read_register (IPSW_REGNUM));
488 sp = push_word (sp, read_register (SAR_REGNUM));
489 sp = push_word (sp, read_register (PCOQ_HEAD_REGNUM));
490 sp = push_word (sp, read_register (PCSQ_HEAD_REGNUM));
491 sp = push_word (sp, read_register (PCOQ_TAIL_REGNUM));
492 sp = push_word (sp, read_register (PCSQ_TAIL_REGNUM));
493 write_register (SP_REGNUM, sp);
496 find_dummy_frame_regs (frame, frame_saved_regs)
497 struct frame_info *frame;
498 struct frame_saved_regs *frame_saved_regs;
500 CORE_ADDR fp = frame->frame;
503 frame_saved_regs->regs[RP_REGNUM] = fp - 20 & ~0x3;
504 frame_saved_regs->regs[FP_REGNUM] = fp;
505 frame_saved_regs->regs[1] = fp + 8;
507 for (fp += 12, i = 3; i < 32; i++)
511 frame_saved_regs->regs[i] = fp;
517 for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
518 frame_saved_regs->regs[i] = fp;
520 frame_saved_regs->regs[IPSW_REGNUM] = fp;
521 frame_saved_regs->regs[SAR_REGNUM] = fp + 4;
522 frame_saved_regs->regs[PCOQ_HEAD_REGNUM] = fp + 8;
523 frame_saved_regs->regs[PCSQ_HEAD_REGNUM] = fp + 12;
524 frame_saved_regs->regs[PCOQ_TAIL_REGNUM] = fp + 16;
525 frame_saved_regs->regs[PCSQ_TAIL_REGNUM] = fp + 20;
531 register FRAME frame = get_current_frame ();
532 register CORE_ADDR fp;
534 struct frame_saved_regs fsr;
535 struct frame_info *fi;
538 fi = get_frame_info (frame);
540 get_frame_saved_regs (fi, &fsr);
542 if (fsr.regs[IPSW_REGNUM]) /* Restoring a call dummy frame */
543 restore_pc_queue (&fsr);
545 for (regnum = 31; regnum > 0; regnum--)
546 if (fsr.regs[regnum])
547 write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
549 for (regnum = NUM_REGS - 1; regnum >= FP0_REGNUM ; regnum--)
550 if (fsr.regs[regnum])
552 read_memory (fsr.regs[regnum], (char *)&freg_buffer, 8);
553 write_register_bytes (REGISTER_BYTE (regnum), (char *)&freg_buffer, 8);
556 if (fsr.regs[IPSW_REGNUM])
557 write_register (IPSW_REGNUM,
558 read_memory_integer (fsr.regs[IPSW_REGNUM], 4));
560 if (fsr.regs[SAR_REGNUM])
561 write_register (SAR_REGNUM,
562 read_memory_integer (fsr.regs[SAR_REGNUM], 4));
564 if (fsr.regs[PCOQ_TAIL_REGNUM])
565 write_register (PCOQ_TAIL_REGNUM,
566 read_memory_integer (fsr.regs[PCOQ_TAIL_REGNUM], 4));
568 write_register (FP_REGNUM, read_memory_integer (fp, 4));
570 if (fsr.regs[IPSW_REGNUM]) /* call dummy */
571 write_register (SP_REGNUM, fp - 48);
573 write_register (SP_REGNUM, fp);
575 flush_cached_frames ();
576 set_current_frame (create_new_frame (read_register (FP_REGNUM),
581 * After returning to a dummy on the stack, restore the instruction
582 * queue space registers. */
585 restore_pc_queue (fsr)
586 struct frame_saved_regs *fsr;
588 CORE_ADDR pc = read_pc ();
589 CORE_ADDR new_pc = read_memory_integer (fsr->regs[PCOQ_HEAD_REGNUM], 4);
594 /* Advance past break instruction in the call dummy. */
595 write_register (PCOQ_HEAD_REGNUM, pc + 4);
596 write_register (PCOQ_TAIL_REGNUM, pc + 8);
599 * HPUX doesn't let us set the space registers or the space
600 * registers of the PC queue through ptrace. Boo, hiss.
601 * Conveniently, the call dummy has this sequence of instructions
606 * So, load up the registers and single step until we are in the
610 write_register (21, read_memory_integer (fsr->regs[PCSQ_HEAD_REGNUM], 4));
611 write_register (22, new_pc);
613 for (insn_count = 0; insn_count < 3; insn_count++)
620 stop_signal = WTERMSIG (w);
621 terminal_ours_for_output ();
622 printf ("\nProgram terminated with signal %d, %s\n",
623 stop_signal, safe_strsignal (stop_signal));
628 fetch_inferior_registers (-1);
633 hppa_push_arguments (nargs, args, sp, struct_return, struct_addr)
638 CORE_ADDR struct_addr;
640 /* array of arguments' offsets */
641 int *offset = (int *)alloca(nargs);
645 for (i = 0; i < nargs; i++)
647 /* Coerce chars to int & float to double if necessary */
648 args[i] = value_arg_coerce (args[i]);
650 cum += TYPE_LENGTH (VALUE_TYPE (args[i]));
652 /* value must go at proper alignment. Assume alignment is a
654 alignment = hppa_alignof (VALUE_TYPE (args[i]));
656 cum = (cum + alignment) & -alignment;
659 sp += min ((cum + 7) & -8, 16);
661 for (i = 0; i < nargs; i++)
662 write_memory (sp + offset[i], VALUE_CONTENTS (args[i]),
663 TYPE_LENGTH (VALUE_TYPE (args[i])));
666 write_register (28, struct_addr);
671 * Insert the specified number of args and function address
672 * into a call sequence of the above form stored at DUMMYNAME.
674 * On the hppa we need to call the stack dummy through $$dyncall.
675 * Therefore our version of FIX_CALL_DUMMY takes an extra argument,
676 * real_pc, which is the location where gdb should start up the
677 * inferior to do the function call.
681 hppa_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
682 REGISTER_TYPE *dummy;
690 CORE_ADDR dyncall_addr, sr4export_addr;
691 struct minimal_symbol *msymbol;
693 msymbol = lookup_minimal_symbol ("$$dyncall", (struct objfile *) NULL);
695 error ("Can't find an address for $$dyncall trampoline");
697 dyncall_addr = SYMBOL_VALUE_ADDRESS (msymbol);
699 msymbol = lookup_minimal_symbol ("_sr4export", (struct objfile *) NULL);
701 error ("Can't find an address for _sr4export trampoline");
703 sr4export_addr = SYMBOL_VALUE_ADDRESS (msymbol);
705 dummy[9] = deposit_21 (fun >> 11, dummy[9]);
706 dummy[10] = deposit_14 (fun & MASK_11, dummy[10]);
707 dummy[12] = deposit_21 (sr4export_addr >> 11, dummy[12]);
708 dummy[13] = deposit_14 (sr4export_addr & MASK_11, dummy[13]);
710 write_register (22, pc);
715 /* return the alignment of a type in bytes. Structures have the maximum
716 alignment required by their fields. */
722 int max_align, align, i;
723 switch (TYPE_CODE (arg))
728 return TYPE_LENGTH (arg);
729 case TYPE_CODE_ARRAY:
730 return hppa_alignof (TYPE_FIELD_TYPE (arg, 0));
731 case TYPE_CODE_STRUCT:
732 case TYPE_CODE_UNION:
734 for (i = 0; i < TYPE_NFIELDS (arg); i++)
736 /* Bit fields have no real alignment. */
737 if (!TYPE_FIELD_BITPOS (arg, i))
739 align = hppa_alignof (TYPE_FIELD_TYPE (arg, i));
740 max_align = max (max_align, align);
749 /* Print the register regnum, or all registers if regnum is -1 */
751 pa_do_registers_info (regnum, fpregs)
755 char raw_regs [REGISTER_BYTES];
758 for (i = 0; i < NUM_REGS; i++)
759 read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i));
761 pa_print_registers (raw_regs, regnum, fpregs);
762 else if (regnum < FP0_REGNUM)
763 printf ("%s %x\n", reg_names[regnum], *(long *)(raw_regs +
764 REGISTER_BYTE (regnum)));
766 pa_print_fp_reg (regnum);
769 pa_print_registers (raw_regs, regnum, fpregs)
776 for (i = 0; i < 18; i++)
777 printf ("%8.8s: %8x %8.8s: %8x %8.8s: %8x %8.8s: %8x\n",
779 *(int *)(raw_regs + REGISTER_BYTE (i)),
781 *(int *)(raw_regs + REGISTER_BYTE (i + 18)),
783 *(int *)(raw_regs + REGISTER_BYTE (i + 36)),
785 *(int *)(raw_regs + REGISTER_BYTE (i + 54)));
788 for (i = 72; i < NUM_REGS; i++)
795 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
796 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
799 /* Get the data in raw format, then convert also to virtual format. */
800 read_relative_register_raw_bytes (i, raw_buffer);
801 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
803 fputs_filtered (reg_names[i], stdout);
804 print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
806 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, stdout, 0,
807 1, 0, Val_pretty_default);
808 printf_filtered ("\n");
811 /* Function calls that pass into a new compilation unit must pass through a
812 small piece of code that does long format (`external' in HPPA parlance)
813 jumps. We figure out where the trampoline is going to end up, and return
814 the PC of the final destination. If we aren't in a trampoline, we just
817 For computed calls, we just extract the new PC from r22. */
820 skip_trampoline_code (pc, name)
825 static CORE_ADDR dyncall = 0;
826 struct minimal_symbol *msym;
828 /* FIXME XXX - dyncall must be initialized whenever we get a new exec file */
832 msym = lookup_minimal_symbol ("$$dyncall", NULL);
834 dyncall = SYMBOL_VALUE_ADDRESS (msym);
840 return (CORE_ADDR)(read_register (22) & ~0x3);
842 inst0 = read_memory_integer (pc, 4);
843 inst1 = read_memory_integer (pc+4, 4);
845 if ( (inst0 & 0xffe00000) == 0x20200000 /* ldil xxx, r1 */
846 && (inst1 & 0xffe0e002) == 0xe0202002) /* be,n yyy(sr4, r1) */
847 pc = extract_21 (inst0) + extract_17 (inst1);
849 pc = (CORE_ADDR)NULL;
854 /* Advance PC across any function entry prologue instructions
855 to reach some "real" code. */
857 /* skip (stw rp, -20(0,sp)); copy 4,1; copy sp, 4; stwm 1,framesize(sp)
858 for gcc, or (stw rp, -20(0,sp); stwm 1, framesize(sp) for hcc */
867 status = target_read_memory (pc, (char *)&inst, 4);
868 SWAP_TARGET_AND_HOST (&inst, sizeof (inst));
872 if (inst == 0x6BC23FD9) /* stw rp,-20(sp) */
874 if (read_memory_integer (pc + 4, 4) == 0x8040241) /* copy r4,r1 */
876 else if ((read_memory_integer (pc + 4, 4) & ~MASK_14) == 0x68810000) /* stw r1,(r4) */
879 else if (read_memory_integer (pc, 4) == 0x8040241) /* copy r4,r1 */
881 else if ((read_memory_integer (pc, 4) & ~MASK_14) == 0x68810000) /* stw r1,(r4) */
888 unwind_command (exp, from_tty)
896 struct unwind_table_entry *u;
899 /* If we have an expression, evaluate it and use it as the address. */
901 if (exp != 0 && *exp != 0)
902 address = parse_and_eval_address (exp);
906 xxx.u = find_unwind_entry (address);
910 printf ("Can't find unwind table entry for PC 0x%x\n", address);
914 printf ("%08x\n%08X\n%08X\n%08X\n", xxx.foo[0], xxx.foo[1], xxx.foo[2],
919 _initialize_hppah_tdep ()
921 add_com ("unwind", class_obscure, unwind_command, "Print unwind info\n");
923 (add_set_cmd ("use_unwind", class_obscure, var_boolean,
925 "Set the usage of unwind info", &setlist),