1 /* Target-machine dependent code for the AMD 29000
2 Copyright 1990, 1991 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Jim Kingdon.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /*#include <sys/param.h> */
31 extern CORE_ADDR text_start
; /* FIXME, kludge... */
33 /* The user-settable top of the register stack in virtual memory. We
34 won't attempt to access any stored registers above this address, if set
37 static CORE_ADDR rstack_high_address
= UINT_MAX
;
39 /* Structure to hold cached info about function prologues. */
42 CORE_ADDR pc
; /* First addr after fn prologue */
43 unsigned rsize
, msize
; /* register stack frame size, mem stack ditto */
44 unsigned mfp_used
: 1; /* memory frame pointer used */
45 unsigned rsize_valid
: 1; /* Validity bits for the above */
46 unsigned msize_valid
: 1;
47 unsigned mfp_valid
: 1;
50 /* Examine the prologue of a function which starts at PC. Return
51 the first addess past the prologue. If MSIZE is non-NULL, then
52 set *MSIZE to the memory stack frame size. If RSIZE is non-NULL,
53 then set *RSIZE to the register stack frame size (not including
54 incoming arguments and the return address & frame pointer stored
55 with them). If no prologue is found, *RSIZE is set to zero.
56 If no prologue is found, or a prologue which doesn't involve
57 allocating a memory stack frame, then set *MSIZE to zero.
59 Note that both msize and rsize are in bytes. This is not consistent
60 with the _User's Manual_ with respect to rsize, but it is much more
63 If MFP_USED is non-NULL, *MFP_USED is set to nonzero if a memory
64 frame pointer is being used. */
66 examine_prologue (pc
, rsize
, msize
, mfp_used
)
74 struct minimal_symbol
*msymbol
= lookup_minimal_symbol_by_pc (pc
);
75 struct prologue_info
*mi
= 0;
78 mi
= (struct prologue_info
*) msymbol
-> info
;
86 valid
&= mi
->rsize_valid
;
91 valid
&= mi
->msize_valid
;
95 *mfp_used
= mi
->mfp_used
;
96 valid
&= mi
->mfp_valid
;
106 if (mfp_used
!= NULL
)
109 /* Prologue must start with subtracting a constant from gr1.
110 Normally this is sub gr1,gr1,<rsize * 4>. */
111 insn
= read_memory_integer (p
, 4);
112 if ((insn
& 0xffffff00) != 0x25010100)
114 /* If the frame is large, instead of a single instruction it
115 might be a pair of instructions:
116 const <reg>, <rsize * 4>
120 /* Possible value for rsize. */
123 if ((insn
& 0xff000000) != 0x03000000)
128 reg
= (insn
>> 8) & 0xff;
129 rsize0
= (((insn
>> 8) & 0xff00) | (insn
& 0xff));
131 insn
= read_memory_integer (p
, 4);
132 if ((insn
& 0xffffff00) != 0x24010100
133 || (insn
& 0xff) != reg
)
144 *rsize
= (insn
& 0xff);
148 /* Next instruction must be asgeu V_SPILL,gr1,rab.
149 * We don't check the vector number to allow for kernel debugging. The
150 * kernel will use a different trap number.
152 insn
= read_memory_integer (p
, 4);
153 if ((insn
& 0xff00ffff) != (0x5e000100|RAB_HW_REGNUM
))
160 /* Next instruction usually sets the frame pointer (lr1) by adding
161 <size * 4> from gr1. However, this can (and high C does) be
162 deferred until anytime before the first function call. So it is
163 OK if we don't see anything which sets lr1.
164 To allow for alternate register sets (gcc -mkernel-registers) the msp
165 register number is a compile time constant. */
167 /* Normally this is just add lr1,gr1,<size * 4>. */
168 insn
= read_memory_integer (p
, 4);
169 if ((insn
& 0xffffff00) == 0x15810100)
173 /* However, for large frames it can be
174 const <reg>, <size *4>
180 if ((insn
& 0xff000000) == 0x03000000)
182 reg
= (insn
>> 8) & 0xff;
184 insn
= read_memory_integer (q
, 4);
185 if ((insn
& 0xffffff00) == 0x14810100
186 && (insn
& 0xff) == reg
)
191 /* Next comes "add lr{<rsize-1>},msp,0", but only if a memory
192 frame pointer is in use. We just check for add lr<anything>,msp,0;
193 we don't check this rsize against the first instruction, and
194 we don't check that the trace-back tag indicates a memory frame pointer
196 To allow for alternate register sets (gcc -mkernel-registers) the msp
197 register number is a compile time constant.
199 The recommended instruction is actually "sll lr<whatever>,msp,0".
200 We check for that, too. Originally Jim Kingdon's code seemed
201 to be looking for a "sub" instruction here, but the mask was set
202 up to lose all the time. */
203 insn
= read_memory_integer (p
, 4);
204 if (((insn
& 0xff80ffff) == (0x15800000|(MSP_HW_REGNUM
<<8))) /* add */
205 || ((insn
& 0xff80ffff) == (0x81800000|(MSP_HW_REGNUM
<<8)))) /* sll */
208 if (mfp_used
!= NULL
)
212 /* Next comes a subtraction from msp to allocate a memory frame,
213 but only if a memory frame is
214 being used. We don't check msize against the trace-back tag.
216 To allow for alternate register sets (gcc -mkernel-registers) the msp
217 register number is a compile time constant.
219 Normally this is just
222 insn
= read_memory_integer (p
, 4);
223 if ((insn
& 0xffffff00) ==
224 (0x25000000|(MSP_HW_REGNUM
<<16)|(MSP_HW_REGNUM
<<8)))
228 *msize
= insn
& 0xff;
232 /* For large frames, instead of a single instruction it might
236 consth <reg>, <msize> ; optional
243 if ((insn
& 0xff000000) == 0x03000000)
245 reg
= (insn
>> 8) & 0xff;
246 msize0
= ((insn
>> 8) & 0xff00) | (insn
& 0xff);
248 insn
= read_memory_integer (q
, 4);
249 /* Check for consth. */
250 if ((insn
& 0xff000000) == 0x02000000
251 && (insn
& 0x0000ff00) == reg
)
253 msize0
|= (insn
<< 8) & 0xff000000;
254 msize0
|= (insn
<< 16) & 0x00ff0000;
256 insn
= read_memory_integer (q
, 4);
258 /* Check for sub msp,msp,<reg>. */
259 if ((insn
& 0xffffff00) ==
260 (0x24000000|(MSP_HW_REGNUM
<<16)|(MSP_HW_REGNUM
<<8))
261 && (insn
& 0xff) == reg
)
275 /* Add a new cache entry. */
276 mi
= (struct prologue_info
*)xmalloc (sizeof (struct prologue_info
));
277 msymbol
-> info
= (char *)mi
;
282 /* else, cache entry exists, but info is incomplete. */
294 if (mfp_used
!= NULL
)
296 mi
->mfp_used
= *mfp_used
;
303 /* Advance PC across any function entry prologue instructions
304 to reach some "real" code. */
310 return examine_prologue (pc
, (unsigned *)NULL
, (unsigned *)NULL
,
314 * Examine the one or two word tag at the beginning of a function.
315 * The tag word is expect to be at 'p', if it is not there, we fail
316 * by returning 0. The documentation for the tag word was taken from
317 * page 7-15 of the 29050 User's Manual. We are assuming that the
318 * m bit is in bit 22 of the tag word, which seems to be the agreed upon
319 * convention today (1/15/92).
320 * msize is return in bytes.
322 static int /* 0/1 - failure/success of finding the tag word */
323 examine_tag(p
, is_trans
, argcount
, msize
, mfp_used
)
330 unsigned int tag1
, tag2
;
332 tag1
= read_memory_integer (p
, 4);
333 if ((tag1
& 0xff000000) != 0) /* Not a tag word */
335 if (tag1
& (1<<23)) /* A two word tag */
337 tag2
= read_memory_integer (p
+4, 4);
341 else /* A one word tag */
344 *msize
= tag1
& 0x7ff;
347 *is_trans
= ((tag1
& (1<<21)) ? 1 : 0);
349 *argcount
= (tag1
>> 16) & 0x1f;
351 *mfp_used
= ((tag1
& (1<<22)) ? 1 : 0);
355 /* Initialize the frame. In addition to setting "extra" frame info,
356 we also set ->frame because we use it in a nonstandard way, and ->pc
357 because we need to know it to get the other stuff. See the diagram
358 of stacks and the frame cache in tm-29k.h for more detail. */
360 init_frame_info (innermost_frame
, fci
)
362 struct frame_info
*fci
;
374 fci
->frame
= read_register (GR1_REGNUM
);
376 fci
->frame
= fci
->next_frame
+ fci
->next
->rsize
;
378 #if CALL_DUMMY_LOCATION == ON_STACK
381 if (PC_IN_CALL_DUMMY (p
, 0, 0))
384 fci
->rsize
= DUMMY_FRAME_RSIZE
;
385 /* This doesn't matter since we never try to get locals or args
386 from a dummy frame. */
388 /* Dummy frames always use a memory frame pointer. */
390 read_register_stack_integer (fci
->frame
+ DUMMY_FRAME_RSIZE
- 4, 4);
391 fci
->flags
|= (TRANSPARENT
|MFP_USED
);
395 func
= find_pc_function (p
);
397 p
= BLOCK_START (SYMBOL_BLOCK_VALUE (func
));
400 /* Search backward to find the trace-back tag. However,
401 do not trace back beyond the start of the text segment
402 (just as a sanity check to avoid going into never-never land). */
403 while (p
>= text_start
404 && ((insn
= read_memory_integer (p
, 4)) & 0xff000000) != 0)
409 /* Couldn't find the trace-back tag.
410 Something strange is going on. */
414 fci
->flags
= TRANSPARENT
;
418 /* Advance to the first word of the function, i.e. the word
419 after the trace-back tag. */
422 /* We've found the start of the function.
423 * Try looking for a tag word that indicates whether there is a
424 * memory frame pointer and what the memory stack allocation is.
425 * If one doesn't exist, try using a more exhaustive search of
426 * the prologue. For now we don't care about the argcount or
427 * whether or not the routine is transparent.
429 if (examine_tag(p
-4,&trans
,NULL
,&msize
,&mfp_used
)) /* Found a good tag */
430 examine_prologue (p
, &rsize
, 0, 0);
431 else /* No tag try prologue */
432 examine_prologue (p
, &rsize
, &msize
, &mfp_used
);
438 fci
->flags
|= MFP_USED
;
440 fci
->flags
|= TRANSPARENT
;
443 fci
->saved_msp
= read_register (MSP_REGNUM
) + msize
;
449 read_register_stack_integer (fci
->frame
+ rsize
- 4, 4);
451 fci
->saved_msp
= fci
->next
->saved_msp
+ msize
;
456 init_extra_frame_info (fci
)
457 struct frame_info
*fci
;
460 /* Assume innermost frame. May produce strange results for "info frame"
461 but there isn't any way to tell the difference. */
462 init_frame_info (1, fci
);
464 /* We're in get_prev_frame_info.
465 Take care of everything in init_frame_pc. */
471 init_frame_pc (fromleaf
, fci
)
473 struct frame_info
*fci
;
475 fci
->pc
= (fromleaf
? SAVED_PC_AFTER_CALL (fci
->next
) :
476 fci
->next
? FRAME_SAVED_PC (fci
->next
) : read_pc ());
477 init_frame_info (fromleaf
, fci
);
480 /* Local variables (i.e. LOC_LOCAL) are on the memory stack, with their
481 offsets being relative to the memory stack pointer (high C) or
485 frame_locals_address (fi
)
486 struct frame_info
*fi
;
488 if (fi
->flags
& MFP_USED
)
489 return fi
->saved_msp
;
491 return fi
->saved_msp
- fi
->msize
;
494 /* Routines for reading the register stack. The caller gets to treat
495 the register stack as a uniform stack in memory, from address $gr1
496 straight through $rfb and beyond. */
498 /* Analogous to read_memory except the length is understood to be 4.
499 Also, myaddr can be NULL (meaning don't bother to read), and
500 if actual_mem_addr is non-NULL, store there the address that it
501 was fetched from (or if from a register the offset within
502 registers). Set *LVAL to lval_memory or lval_register, depending
503 on where it came from. */
505 read_register_stack (memaddr
, myaddr
, actual_mem_addr
, lval
)
508 CORE_ADDR
*actual_mem_addr
;
509 enum lval_type
*lval
;
511 long rfb
= read_register (RFB_REGNUM
);
512 long rsp
= read_register (RSP_REGNUM
);
514 /* If we don't do this 'info register' stops in the middle. */
515 if (memaddr
>= rstack_high_address
)
517 int val
=-1; /* a bogus value */
518 /* It's in a local register, but off the end of the stack. */
519 int regnum
= (memaddr
- rsp
) / 4 + LR0_REGNUM
;
521 *(int*)myaddr
= val
; /* Provide bogusness */
522 supply_register(regnum
,&val
); /* More bogusness */
524 *lval
= lval_register
;
525 if (actual_mem_addr
!= NULL
)
526 *actual_mem_addr
= REGISTER_BYTE (regnum
);
528 else if (memaddr
< rfb
)
530 /* It's in a register. */
531 int regnum
= (memaddr
- rsp
) / 4 + LR0_REGNUM
;
532 if (regnum
< LR0_REGNUM
|| regnum
> LR0_REGNUM
+ 127)
533 error ("Attempt to read register stack out of range.");
535 read_register_gen (regnum
, myaddr
);
537 *lval
= lval_register
;
538 if (actual_mem_addr
!= NULL
)
539 *actual_mem_addr
= REGISTER_BYTE (regnum
);
543 /* It's in the memory portion of the register stack. */
545 read_memory (memaddr
, myaddr
, 4);
548 if (actual_mem_addr
!= NULL
)
549 *actual_mem_addr
= memaddr
;
553 /* Analogous to read_memory_integer
554 except the length is understood to be 4. */
556 read_register_stack_integer (memaddr
, len
)
561 read_register_stack (memaddr
, &buf
, NULL
, NULL
);
562 SWAP_TARGET_AND_HOST (&buf
, 4);
566 /* Copy 4 bytes from GDB memory at MYADDR into inferior memory
567 at MEMADDR and put the actual address written into in
570 write_register_stack (memaddr
, myaddr
, actual_mem_addr
)
573 CORE_ADDR
*actual_mem_addr
;
575 long rfb
= read_register (RFB_REGNUM
);
576 long rsp
= read_register (RSP_REGNUM
);
577 /* If we don't do this 'info register' stops in the middle. */
578 if (memaddr
>= rstack_high_address
)
580 /* It's in a register, but off the end of the stack. */
581 if (actual_mem_addr
!= NULL
)
582 *actual_mem_addr
= NULL
;
584 else if (memaddr
< rfb
)
586 /* It's in a register. */
587 int regnum
= (memaddr
- rsp
) / 4 + LR0_REGNUM
;
588 if (regnum
< LR0_REGNUM
|| regnum
> LR0_REGNUM
+ 127)
589 error ("Attempt to read register stack out of range.");
591 write_register (regnum
, *(long *)myaddr
);
592 if (actual_mem_addr
!= NULL
)
593 *actual_mem_addr
= NULL
;
597 /* It's in the memory portion of the register stack. */
599 write_memory (memaddr
, myaddr
, 4);
600 if (actual_mem_addr
!= NULL
)
601 *actual_mem_addr
= memaddr
;
605 /* Find register number REGNUM relative to FRAME and put its
606 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
607 was optimized out (and thus can't be fetched). If the variable
608 was fetched from memory, set *ADDRP to where it was fetched from,
609 otherwise it was fetched from a register.
611 The argument RAW_BUFFER must point to aligned memory. */
613 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lvalp
)
619 enum lval_type
*lvalp
;
621 struct frame_info
*fi
;
628 fi
= get_frame_info (frame
);
630 /* Once something has a register number, it doesn't get optimized out. */
631 if (optimized
!= NULL
)
633 if (regnum
== RSP_REGNUM
)
635 if (raw_buffer
!= NULL
)
636 *(CORE_ADDR
*)raw_buffer
= fi
->frame
;
641 else if (regnum
== PC_REGNUM
)
643 if (raw_buffer
!= NULL
)
644 *(CORE_ADDR
*)raw_buffer
= fi
->pc
;
646 /* Not sure we have to do this. */
652 else if (regnum
== MSP_REGNUM
)
654 if (raw_buffer
!= NULL
)
656 if (fi
->next
!= NULL
)
657 *(CORE_ADDR
*)raw_buffer
= fi
->next
->saved_msp
;
659 *(CORE_ADDR
*)raw_buffer
= read_register (MSP_REGNUM
);
661 /* The value may have been computed, not fetched. */
666 else if (regnum
< LR0_REGNUM
|| regnum
>= LR0_REGNUM
+ 128)
668 /* These registers are not saved over procedure calls,
669 so just print out the current values. */
670 if (raw_buffer
!= NULL
)
671 *(CORE_ADDR
*)raw_buffer
= read_register (regnum
);
673 *lvalp
= lval_register
;
675 *addrp
= REGISTER_BYTE (regnum
);
679 addr
= fi
->frame
+ (regnum
- LR0_REGNUM
) * 4;
680 if (raw_buffer
!= NULL
)
681 read_register_stack (addr
, raw_buffer
, &addr
, &lval
);
689 /* Discard from the stack the innermost frame,
690 restoring all saved registers. */
695 FRAME frame
= get_current_frame ();
696 struct frame_info
*fi
= get_frame_info (frame
);
697 CORE_ADDR rfb
= read_register (RFB_REGNUM
);
698 CORE_ADDR gr1
= fi
->frame
+ fi
->rsize
;
702 /* If popping a dummy frame, need to restore registers. */
703 if (PC_IN_CALL_DUMMY (read_register (PC_REGNUM
),
704 read_register (SP_REGNUM
),
707 int lrnum
= LR0_REGNUM
+ DUMMY_ARG
/4;
708 for (i
= 0; i
< DUMMY_SAVE_SR128
; ++i
)
709 write_register (SR_REGNUM (i
+ 128),read_register (lrnum
++));
710 for (i
= 0; i
< DUMMY_SAVE_SR160
; ++i
)
711 write_register (SR_REGNUM(i
+160), read_register (lrnum
++));
712 for (i
= 0; i
< DUMMY_SAVE_GREGS
; ++i
)
713 write_register (RETURN_REGNUM
+ i
, read_register (lrnum
++));
714 /* Restore the PCs. */
715 write_register(PC_REGNUM
, read_register (lrnum
++));
716 write_register(NPC_REGNUM
, read_register (lrnum
));
719 /* Restore the memory stack pointer. */
720 write_register (MSP_REGNUM
, fi
->saved_msp
);
721 /* Restore the register stack pointer. */
722 write_register (GR1_REGNUM
, gr1
);
723 /* Check whether we need to fill registers. */
724 lr1
= read_register (LR0_REGNUM
+ 1);
728 int num_bytes
= lr1
- rfb
;
731 write_register (RAB_REGNUM
, read_register (RAB_REGNUM
) + num_bytes
);
732 write_register (RFB_REGNUM
, lr1
);
733 for (i
= 0; i
< num_bytes
; i
+= 4)
735 /* Note: word is in host byte order. */
736 word
= read_memory_integer (rfb
+ i
, 4);
737 write_register (LR0_REGNUM
+ ((rfb
- gr1
) % 0x80) + i
/ 4, word
);
740 flush_cached_frames ();
741 set_current_frame (create_new_frame (0, read_pc()));
744 /* Push an empty stack frame, to record the current PC, etc. */
751 CORE_ADDR msp
= read_register (MSP_REGNUM
);
752 int lrnum
, i
, saved_lr0
;
755 /* Allocate the new frame. */
756 gr1
= read_register (GR1_REGNUM
) - DUMMY_FRAME_RSIZE
;
757 write_register (GR1_REGNUM
, gr1
);
759 rab
= read_register (RAB_REGNUM
);
762 /* We need to spill registers. */
763 int num_bytes
= rab
- gr1
;
764 CORE_ADDR rfb
= read_register (RFB_REGNUM
);
768 write_register (RFB_REGNUM
, rfb
- num_bytes
);
769 write_register (RAB_REGNUM
, gr1
);
770 for (i
= 0; i
< num_bytes
; i
+= 4)
772 /* Note: word is in target byte order. */
773 read_register_gen (LR0_REGNUM
+ i
/ 4, &word
);
774 write_memory (rfb
- num_bytes
+ i
, &word
, 4);
778 /* There are no arguments in to the dummy frame, so we don't need
779 more than rsize plus the return address and lr1. */
780 write_register (LR0_REGNUM
+ 1, gr1
+ DUMMY_FRAME_RSIZE
+ 2 * 4);
782 /* Set the memory frame pointer. */
783 write_register (LR0_REGNUM
+ DUMMY_FRAME_RSIZE
/ 4 - 1, msp
);
785 /* Allocate arg_slop. */
786 write_register (MSP_REGNUM
, msp
- 16 * 4);
788 /* Save registers. */
789 lrnum
= LR0_REGNUM
+ DUMMY_ARG
/4;
790 for (i
= 0; i
< DUMMY_SAVE_SR128
; ++i
)
791 write_register (lrnum
++, read_register (SR_REGNUM (i
+ 128)));
792 for (i
= 0; i
< DUMMY_SAVE_SR160
; ++i
)
793 write_register (lrnum
++, read_register (SR_REGNUM (i
+ 160)));
794 for (i
= 0; i
< DUMMY_SAVE_GREGS
; ++i
)
795 write_register (lrnum
++, read_register (RETURN_REGNUM
+ i
));
797 write_register (lrnum
++, read_register (PC_REGNUM
));
798 write_register (lrnum
, read_register (NPC_REGNUM
));
801 reginv_com (args
, fromtty
)
807 printf_filtered("Gdb's register cache invalidated.\n");
810 /* We use this mostly for debugging gdb */
814 add_com ("reginv ", class_obscure
, reginv_com
,
815 "Invalidate gdb's internal register cache.");
817 /* FIXME, there should be a way to make a CORE_ADDR variable settable. */
819 (add_set_cmd ("rstack_high_address", class_support
, var_uinteger
,
820 (char *)&rstack_high_address
,
821 "Set top address in memory of the register stack.\n\
822 Attempts to access registers saved above this address will be ignored\n\
823 or will produce the value -1.", &setlist
),