1 /* Target-dependent code for Atmel AVR, for GDB.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Contributed by Theodore A. Roth, troth@verinet.com */
24 /* Portions of this file were taken from the original gdb-4.18 patch developed
25 by Denis Chertykov, denisc@overta.ru */
32 #include "arch-utils.h"
34 #include "gdb_string.h"
38 (AVR micros are pure Harvard Architecture processors.)
40 The AVR family of microcontrollers have three distinctly different memory
41 spaces: flash, sram and eeprom. The flash is 16 bits wide and is used for
42 the most part to store program instructions. The sram is 8 bits wide and is
43 used for the stack and the heap. Some devices lack sram and some can have
44 an additional external sram added on as a peripheral.
46 The eeprom is 8 bits wide and is used to store data when the device is
47 powered down. Eeprom is not directly accessible, it can only be accessed
48 via io-registers using a special algorithm. Accessing eeprom via gdb's
49 remote serial protocol ('m' or 'M' packets) looks difficult to do and is
50 not included at this time.
52 [The eeprom could be read manually via ``x/b <eaddr + AVR_EMEM_START>'' or
53 written using ``set {unsigned char}<eaddr + AVR_EMEM_START>''. For this to
54 work, the remote target must be able to handle eeprom accesses and perform
55 the address translation.]
57 All three memory spaces have physical addresses beginning at 0x0. In
58 addition, the flash is addressed by gcc/binutils/gdb with respect to 8 bit
59 bytes instead of the 16 bit wide words used by the real device for the
62 In order for remote targets to work correctly, extra bits must be added to
63 addresses before they are send to the target or received from the target
64 via the remote serial protocol. The extra bits are the MSBs and are used to
65 decode which memory space the address is referring to. */
68 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
71 #define EXTRACT_INSN(addr) extract_unsigned_integer(addr,2)
73 /* Constants: prefixed with AVR_ to avoid name space clashes */
87 AVR_NUM_REGS
= 32 + 1 /*SREG*/ + 1 /*SP*/ + 1 /*PC*/,
88 AVR_NUM_REG_BYTES
= 32 + 1 /*SREG*/ + 2 /*SP*/ + 4 /*PC*/,
90 AVR_PC_REG_INDEX
= 35, /* index into array of registers */
92 AVR_MAX_PROLOGUE_SIZE
= 56, /* bytes */
94 /* Count of pushed registers. From r2 to r17 (inclusively), r28, r29 */
97 /* Number of the last pushed register. r17 for current avr-gcc */
98 AVR_LAST_PUSHED_REGNUM
= 17,
100 /* FIXME: TRoth/2002-01-??: Can we shift all these memory masks left 8
101 bits? Do these have to match the bfd vma values?. It sure would make
102 things easier in the future if they didn't need to match.
104 Note: I chose these values so as to be consistent with bfd vma
107 TRoth/2002-04-08: There is already a conflict with very large programs
108 in the mega128. The mega128 has 128K instruction bytes (64K words),
109 thus the Most Significant Bit is 0x10000 which gets masked off my
112 The problem manifests itself when trying to set a breakpoint in a
113 function which resides in the upper half of the instruction space and
114 thus requires a 17-bit address.
116 For now, I've just removed the EEPROM mask and changed AVR_MEM_MASK
117 from 0x00ff0000 to 0x00f00000. Eeprom is not accessible from gdb yet,
118 but could be for some remote targets by just adding the correct offset
119 to the address and letting the remote target handle the low-level
120 details of actually accessing the eeprom. */
122 AVR_IMEM_START
= 0x00000000, /* INSN memory */
123 AVR_SMEM_START
= 0x00800000, /* SRAM memory */
125 /* No eeprom mask defined */
126 AVR_MEM_MASK
= 0x00f00000, /* mask to determine memory space */
128 AVR_EMEM_START
= 0x00810000, /* EEPROM memory */
129 AVR_MEM_MASK
= 0x00ff0000, /* mask to determine memory space */
133 /* Any function with a frame looks like this
134 ....... <-SP POINTS HERE
135 LOCALS1 <-FP POINTS HERE
144 struct frame_extra_info
147 CORE_ADDR args_pointer
;
156 /* FIXME: TRoth: is there anything to put here? */
160 /* Lookup the name of a register given it's number. */
163 avr_register_name (int regnum
)
165 static char *register_names
[] = {
166 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
167 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
168 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
169 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
174 if (regnum
>= (sizeof (register_names
) / sizeof (*register_names
)))
176 return register_names
[regnum
];
179 /* Index within `registers' of the first byte of the space for
183 avr_register_byte (int regnum
)
185 if (regnum
< AVR_PC_REGNUM
)
188 return AVR_PC_REG_INDEX
;
191 /* Number of bytes of storage in the actual machine representation for
195 avr_register_raw_size (int regnum
)
209 /* Number of bytes of storage in the program's representation
213 avr_register_virtual_size (int regnum
)
215 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (regnum
));
218 /* Return the GDB type object for the "standard" data type
219 of data in register N. */
222 avr_register_virtual_type (int regnum
)
227 return builtin_type_unsigned_long
;
229 return builtin_type_unsigned_short
;
231 return builtin_type_unsigned_char
;
235 /* Instruction address checks and convertions. */
238 avr_make_iaddr (CORE_ADDR x
)
240 return ((x
) | AVR_IMEM_START
);
244 avr_iaddr_p (CORE_ADDR x
)
246 return (((x
) & AVR_MEM_MASK
) == AVR_IMEM_START
);
249 /* FIXME: TRoth: Really need to use a larger mask for instructions. Some
250 devices are already up to 128KBytes of flash space.
252 TRoth/2002-04-8: See comment above where AVR_IMEM_START is defined. */
255 avr_convert_iaddr_to_raw (CORE_ADDR x
)
257 return ((x
) & 0xffffffff);
260 /* SRAM address checks and convertions. */
263 avr_make_saddr (CORE_ADDR x
)
265 return ((x
) | AVR_SMEM_START
);
269 avr_saddr_p (CORE_ADDR x
)
271 return (((x
) & AVR_MEM_MASK
) == AVR_SMEM_START
);
275 avr_convert_saddr_to_raw (CORE_ADDR x
)
277 return ((x
) & 0xffffffff);
280 /* EEPROM address checks and convertions. I don't know if these will ever
281 actually be used, but I've added them just the same. TRoth */
283 /* TRoth/2002-04-08: Commented out for now to allow fix for problem with large
284 programs in the mega128. */
286 /* static CORE_ADDR */
287 /* avr_make_eaddr (CORE_ADDR x) */
289 /* return ((x) | AVR_EMEM_START); */
293 /* avr_eaddr_p (CORE_ADDR x) */
295 /* return (((x) & AVR_MEM_MASK) == AVR_EMEM_START); */
298 /* static CORE_ADDR */
299 /* avr_convert_eaddr_to_raw (CORE_ADDR x) */
301 /* return ((x) & 0xffffffff); */
304 /* Convert from address to pointer and vice-versa. */
307 avr_address_to_pointer (struct type
*type
, void *buf
, CORE_ADDR addr
)
309 /* Is it a code address? */
310 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_FUNC
311 || TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_METHOD
)
313 store_unsigned_integer (buf
, TYPE_LENGTH (type
),
314 avr_convert_iaddr_to_raw (addr
));
318 /* Strip off any upper segment bits. */
319 store_unsigned_integer (buf
, TYPE_LENGTH (type
),
320 avr_convert_saddr_to_raw (addr
));
325 avr_pointer_to_address (struct type
*type
, const void *buf
)
327 CORE_ADDR addr
= extract_address (buf
, TYPE_LENGTH (type
));
329 if (TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type
)))
331 fprintf_unfiltered (gdb_stderr
, "CODE_SPACE ---->> ptr->addr: 0x%lx\n",
333 fprintf_unfiltered (gdb_stderr
,
334 "+++ If you see this, please send me an email <troth@verinet.com>\n");
337 /* Is it a code address? */
338 if (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_FUNC
339 || TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_METHOD
340 || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type
)))
341 return avr_make_iaddr (addr
);
343 return avr_make_saddr (addr
);
347 avr_read_pc (ptid_t ptid
)
353 save_ptid
= inferior_ptid
;
354 inferior_ptid
= ptid
;
355 pc
= (int) read_register (AVR_PC_REGNUM
);
356 inferior_ptid
= save_ptid
;
357 retval
= avr_make_iaddr (pc
);
362 avr_write_pc (CORE_ADDR val
, ptid_t ptid
)
366 save_ptid
= inferior_ptid
;
367 inferior_ptid
= ptid
;
368 write_register (AVR_PC_REGNUM
, avr_convert_iaddr_to_raw (val
));
369 inferior_ptid
= save_ptid
;
375 return (avr_make_saddr (read_register (AVR_SP_REGNUM
)));
379 avr_write_sp (CORE_ADDR val
)
381 write_register (AVR_SP_REGNUM
, avr_convert_saddr_to_raw (val
));
387 return (avr_make_saddr (read_register (AVR_FP_REGNUM
)));
390 /* Translate a GDB virtual ADDR/LEN into a format the remote target
391 understands. Returns number of bytes that can be transfered
392 starting at TARG_ADDR. Return ZERO if no bytes can be transfered
393 (segmentation fault).
395 TRoth/2002-04-08: Could this be used to check for dereferencing an invalid
399 avr_remote_translate_xfer_address (CORE_ADDR memaddr
, int nr_bytes
,
400 CORE_ADDR
*targ_addr
, int *targ_len
)
405 /* FIXME: TRoth: Do nothing for now. Will need to examine memaddr at this
406 point and see if the high bit are set with the masks that we want. */
408 *targ_addr
= memaddr
;
409 *targ_len
= nr_bytes
;
412 /* Function pointers obtained from the target are half of what gdb expects so
416 avr_convert_from_func_ptr_addr (CORE_ADDR addr
)
421 /* avr_scan_prologue is also used as the
422 deprecated_frame_init_saved_regs().
424 Put here the code to store, into fi->saved_regs, the addresses of
425 the saved registers of frame described by FRAME_INFO. This
426 includes special registers such as pc and fp saved in special ways
427 in the stack frame. sp is even more special: the address we return
428 for it IS the sp for the next frame. */
430 /* Function: avr_scan_prologue (helper function for avr_init_extra_frame_info)
431 This function decodes a AVR function prologue to determine:
432 1) the size of the stack frame
433 2) which registers are saved on it
434 3) the offsets of saved regs
435 This information is stored in the "extra_info" field of the frame_info.
437 A typical AVR function prologue might look like this:
443 sbiw r28,<LOCALS_SIZE>
444 in __tmp_reg__,__SREG__
447 out __SREG__,__tmp_reg__
450 A `-mcall-prologues' prologue look like this:
451 ldi r26,<LOCALS_SIZE>
452 ldi r27,<LOCALS_SIZE>/265
453 ldi r30,pm_lo8(.L_foo_body)
454 ldi r31,pm_hi8(.L_foo_body)
455 rjmp __prologue_saves__+RRR
459 avr_scan_prologue (struct frame_info
*fi
)
461 CORE_ADDR prologue_start
;
462 CORE_ADDR prologue_end
;
468 struct minimal_symbol
*msymbol
;
470 unsigned char prologue
[AVR_MAX_PROLOGUE_SIZE
];
473 get_frame_extra_info (fi
)->framereg
= AVR_SP_REGNUM
;
475 if (find_pc_partial_function
476 (get_frame_pc (fi
), &name
, &prologue_start
, &prologue_end
))
478 struct symtab_and_line sal
= find_pc_line (prologue_start
, 0);
480 if (sal
.line
== 0) /* no line info, use current PC */
481 prologue_end
= get_frame_pc (fi
);
482 else if (sal
.end
< prologue_end
) /* next line begins after fn end */
483 prologue_end
= sal
.end
; /* (probably means no prologue) */
486 /* We're in the boondocks: allow for */
487 /* 19 pushes, an add, and "mv fp,sp" */
488 prologue_end
= prologue_start
+ AVR_MAX_PROLOGUE_SIZE
;
490 prologue_end
= min (prologue_end
, get_frame_pc (fi
));
492 /* Search the prologue looking for instructions that set up the
493 frame pointer, adjust the stack pointer, and save registers. */
495 get_frame_extra_info (fi
)->framesize
= 0;
496 prologue_len
= prologue_end
- prologue_start
;
497 read_memory (prologue_start
, prologue
, prologue_len
);
499 /* Scanning main()'s prologue
500 ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>)
501 ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>)
505 if (name
&& strcmp ("main", name
) == 0 && prologue_len
== 8)
508 unsigned char img
[] = {
509 0xde, 0xbf, /* out __SP_H__,r29 */
510 0xcd, 0xbf /* out __SP_L__,r28 */
513 get_frame_extra_info (fi
)->framereg
= AVR_FP_REGNUM
;
514 insn
= EXTRACT_INSN (&prologue
[vpc
]);
515 /* ldi r28,lo8(<RAM_ADDR> - <LOCALS_SIZE>) */
516 if ((insn
& 0xf0f0) == 0xe0c0)
518 locals
= (insn
& 0xf) | ((insn
& 0x0f00) >> 4);
519 insn
= EXTRACT_INSN (&prologue
[vpc
+ 2]);
520 /* ldi r29,hi8(<RAM_ADDR> - <LOCALS_SIZE>) */
521 if ((insn
& 0xf0f0) == 0xe0d0)
523 locals
|= ((insn
& 0xf) | ((insn
& 0x0f00) >> 4)) << 8;
524 if (memcmp (prologue
+ vpc
+ 4, img
, sizeof (img
)) == 0)
526 deprecated_update_frame_base_hack (fi
, locals
);
528 get_frame_extra_info (fi
)->is_main
= 1;
535 /* Scanning `-mcall-prologues' prologue
536 FIXME: mega prologue have a 12 bytes long */
538 while (prologue_len
<= 12) /* I'm use while to avoit many goto's */
544 insn
= EXTRACT_INSN (&prologue
[vpc
]);
545 /* ldi r26,<LOCALS_SIZE> */
546 if ((insn
& 0xf0f0) != 0xe0a0)
548 loc_size
= (insn
& 0xf) | ((insn
& 0x0f00) >> 4);
550 insn
= EXTRACT_INSN (&prologue
[vpc
+ 2]);
551 /* ldi r27,<LOCALS_SIZE> / 256 */
552 if ((insn
& 0xf0f0) != 0xe0b0)
554 loc_size
|= ((insn
& 0xf) | ((insn
& 0x0f00) >> 4)) << 8;
556 insn
= EXTRACT_INSN (&prologue
[vpc
+ 4]);
557 /* ldi r30,pm_lo8(.L_foo_body) */
558 if ((insn
& 0xf0f0) != 0xe0e0)
560 body_addr
= (insn
& 0xf) | ((insn
& 0x0f00) >> 4);
562 insn
= EXTRACT_INSN (&prologue
[vpc
+ 6]);
563 /* ldi r31,pm_hi8(.L_foo_body) */
564 if ((insn
& 0xf0f0) != 0xe0f0)
566 body_addr
|= ((insn
& 0xf) | ((insn
& 0x0f00) >> 4)) << 8;
568 if (body_addr
!= (prologue_start
+ 10) / 2)
571 msymbol
= lookup_minimal_symbol ("__prologue_saves__", NULL
, NULL
);
575 /* FIXME: prologue for mega have a JMP instead of RJMP */
576 insn
= EXTRACT_INSN (&prologue
[vpc
+ 8]);
577 /* rjmp __prologue_saves__+RRR */
578 if ((insn
& 0xf000) != 0xc000)
581 /* Extract PC relative offset from RJMP */
582 i
= (insn
& 0xfff) | (insn
& 0x800 ? (-1 ^ 0xfff) : 0);
583 /* Convert offset to byte addressable mode */
585 /* Destination address */
586 i
+= vpc
+ prologue_start
+ 10;
587 /* Resovle offset (in words) from __prologue_saves__ symbol.
588 Which is a pushes count in `-mcall-prologues' mode */
589 num_pushes
= AVR_MAX_PUSHES
- (i
- SYMBOL_VALUE_ADDRESS (msymbol
)) / 2;
591 if (num_pushes
> AVR_MAX_PUSHES
)
597 get_frame_saved_regs (fi
)[AVR_FP_REGNUM
+ 1] = num_pushes
;
599 get_frame_saved_regs (fi
)[AVR_FP_REGNUM
] = num_pushes
- 1;
601 for (from
= AVR_LAST_PUSHED_REGNUM
+ 1 - (num_pushes
- 2);
602 from
<= AVR_LAST_PUSHED_REGNUM
; ++from
)
603 get_frame_saved_regs (fi
)[from
] = ++i
;
605 get_frame_extra_info (fi
)->locals_size
= loc_size
;
606 get_frame_extra_info (fi
)->framesize
= loc_size
+ num_pushes
;
607 get_frame_extra_info (fi
)->framereg
= AVR_FP_REGNUM
;
611 /* Scan interrupt or signal function */
613 if (prologue_len
>= 12)
615 unsigned char img
[] = {
616 0x78, 0x94, /* sei */
617 0x1f, 0x92, /* push r1 */
618 0x0f, 0x92, /* push r0 */
619 0x0f, 0xb6, /* in r0,0x3f SREG */
620 0x0f, 0x92, /* push r0 */
621 0x11, 0x24 /* clr r1 */
623 if (memcmp (prologue
, img
, sizeof (img
)) == 0)
626 get_frame_saved_regs (fi
)[0] = 2;
627 get_frame_saved_regs (fi
)[1] = 1;
628 get_frame_extra_info (fi
)->framesize
+= 3;
630 else if (memcmp (img
+ 1, prologue
, sizeof (img
) - 1) == 0)
632 vpc
+= sizeof (img
) - 1;
633 get_frame_saved_regs (fi
)[0] = 2;
634 get_frame_saved_regs (fi
)[1] = 1;
635 get_frame_extra_info (fi
)->framesize
+= 3;
639 /* First stage of the prologue scanning.
642 for (; vpc
<= prologue_len
; vpc
+= 2)
644 insn
= EXTRACT_INSN (&prologue
[vpc
]);
645 if ((insn
& 0xfe0f) == 0x920f) /* push rXX */
647 /* Bits 4-9 contain a mask for registers R0-R32. */
648 regno
= (insn
& 0x1f0) >> 4;
649 ++get_frame_extra_info (fi
)->framesize
;
650 get_frame_saved_regs (fi
)[regno
] = get_frame_extra_info (fi
)->framesize
;
657 /* Second stage of the prologue scanning.
662 if (scan_stage
== 1 && vpc
+ 4 <= prologue_len
)
664 unsigned char img
[] = {
665 0xcd, 0xb7, /* in r28,__SP_L__ */
666 0xde, 0xb7 /* in r29,__SP_H__ */
668 unsigned short insn1
;
670 if (memcmp (prologue
+ vpc
, img
, sizeof (img
)) == 0)
673 get_frame_extra_info (fi
)->framereg
= AVR_FP_REGNUM
;
678 /* Third stage of the prologue scanning. (Really two stages)
680 sbiw r28,XX or subi r28,lo8(XX)
682 in __tmp_reg__,__SREG__
685 out __SREG__,__tmp_reg__
688 if (scan_stage
== 2 && vpc
+ 12 <= prologue_len
)
691 unsigned char img
[] = {
692 0x0f, 0xb6, /* in r0,0x3f */
693 0xf8, 0x94, /* cli */
694 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
695 0x0f, 0xbe, /* out 0x3f,r0 ; SREG */
696 0xde, 0xbf /* out 0x3e,r29 ; SPH */
698 unsigned char img_sig
[] = {
699 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
700 0xde, 0xbf /* out 0x3e,r29 ; SPH */
702 unsigned char img_int
[] = {
703 0xf8, 0x94, /* cli */
704 0xcd, 0xbf, /* out 0x3d,r28 ; SPL */
705 0x78, 0x94, /* sei */
706 0xde, 0xbf /* out 0x3e,r29 ; SPH */
709 insn
= EXTRACT_INSN (&prologue
[vpc
]);
711 if ((insn
& 0xff30) == 0x9720) /* sbiw r28,XXX */
712 locals_size
= (insn
& 0xf) | ((insn
& 0xc0) >> 2);
713 else if ((insn
& 0xf0f0) == 0x50c0) /* subi r28,lo8(XX) */
715 locals_size
= (insn
& 0xf) | ((insn
& 0xf00) >> 4);
716 insn
= EXTRACT_INSN (&prologue
[vpc
]);
718 locals_size
+= ((insn
& 0xf) | ((insn
& 0xf00) >> 4) << 8);
722 get_frame_extra_info (fi
)->locals_size
= locals_size
;
723 get_frame_extra_info (fi
)->framesize
+= locals_size
;
727 /* This function actually figures out the frame address for a given pc and
728 sp. This is tricky because we sometimes don't use an explicit
729 frame pointer, and the previous stack pointer isn't necessarily recorded
730 on the stack. The only reliable way to get this info is to
731 examine the prologue. */
734 avr_init_extra_frame_info (int fromleaf
, struct frame_info
*fi
)
738 if (get_next_frame (fi
))
739 deprecated_update_frame_pc_hack (fi
, DEPRECATED_FRAME_SAVED_PC (get_next_frame (fi
)));
741 frame_extra_info_zalloc (fi
, sizeof (struct frame_extra_info
));
742 frame_saved_regs_zalloc (fi
);
744 get_frame_extra_info (fi
)->return_pc
= 0;
745 get_frame_extra_info (fi
)->args_pointer
= 0;
746 get_frame_extra_info (fi
)->locals_size
= 0;
747 get_frame_extra_info (fi
)->framereg
= 0;
748 get_frame_extra_info (fi
)->framesize
= 0;
749 get_frame_extra_info (fi
)->is_main
= 0;
751 avr_scan_prologue (fi
);
753 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi
), get_frame_base (fi
),
754 get_frame_base (fi
)))
756 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
757 by assuming it's always FP. */
758 deprecated_update_frame_base_hack (fi
, deprecated_read_register_dummy (get_frame_pc (fi
), get_frame_base (fi
),
761 else if (!get_next_frame (fi
))
762 /* this is the innermost frame? */
763 deprecated_update_frame_base_hack (fi
, read_register (get_frame_extra_info (fi
)->framereg
));
764 else if (get_frame_extra_info (fi
)->is_main
!= 1)
765 /* not the innermost frame, not `main' */
766 /* If we have an next frame, the callee saved it. */
768 struct frame_info
*next_fi
= get_next_frame (fi
);
769 if (get_frame_extra_info (fi
)->framereg
== AVR_SP_REGNUM
)
770 deprecated_update_frame_base_hack (fi
, (get_frame_base (next_fi
)
772 + get_frame_extra_info (next_fi
)->framesize
));
773 /* FIXME: I don't analyse va_args functions */
778 unsigned int fp_low
, fp_high
;
780 /* Scan all frames */
781 for (; next_fi
; next_fi
= get_next_frame (next_fi
))
783 /* look for saved AVR_FP_REGNUM */
784 if (get_frame_saved_regs (next_fi
)[AVR_FP_REGNUM
] && !fp
)
785 fp
= get_frame_saved_regs (next_fi
)[AVR_FP_REGNUM
];
786 /* look for saved AVR_FP_REGNUM + 1 */
787 if (get_frame_saved_regs (next_fi
)[AVR_FP_REGNUM
+ 1] && !fp1
)
788 fp1
= get_frame_saved_regs (next_fi
)[AVR_FP_REGNUM
+ 1];
790 fp_low
= (fp
? read_memory_unsigned_integer (avr_make_saddr (fp
), 1)
791 : read_register (AVR_FP_REGNUM
)) & 0xff;
793 (fp1
? read_memory_unsigned_integer (avr_make_saddr (fp1
), 1) :
794 read_register (AVR_FP_REGNUM
+ 1)) & 0xff;
795 deprecated_update_frame_base_hack (fi
, fp_low
| (fp_high
<< 8));
799 /* TRoth: Do we want to do this if we are in main? I don't think we should
800 since return_pc makes no sense when we are in main. */
802 if ((get_frame_pc (fi
)) && (get_frame_extra_info (fi
)->is_main
== 0))
803 /* We are not in CALL_DUMMY */
808 addr
= get_frame_base (fi
) + get_frame_extra_info (fi
)->framesize
+ 1;
810 /* Return address in stack in different endianness */
812 get_frame_extra_info (fi
)->return_pc
=
813 read_memory_unsigned_integer (avr_make_saddr (addr
), 1) << 8;
814 get_frame_extra_info (fi
)->return_pc
|=
815 read_memory_unsigned_integer (avr_make_saddr (addr
+ 1), 1);
817 /* This return address in words,
818 must be converted to the bytes address */
819 get_frame_extra_info (fi
)->return_pc
*= 2;
821 /* Resolve a pushed registers addresses */
822 for (i
= 0; i
< NUM_REGS
; i
++)
824 if (get_frame_saved_regs (fi
)[i
])
825 get_frame_saved_regs (fi
)[i
] = addr
- get_frame_saved_regs (fi
)[i
];
830 /* Restore the machine to the state it had before the current frame was
831 created. Usually used either by the "RETURN" command, or by
832 call_function_by_hand after the dummy_frame is finished. */
839 struct frame_info
*frame
= get_current_frame ();
841 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame
),
842 get_frame_base (frame
),
843 get_frame_base (frame
)))
845 generic_pop_dummy_frame ();
849 /* TRoth: Why only loop over 8 registers? */
851 for (regnum
= 0; regnum
< 8; regnum
++)
853 /* Don't forget AVR_SP_REGNUM in a frame_saved_regs struct is the
854 actual value we want, not the address of the value we want. */
855 if (get_frame_saved_regs (frame
)[regnum
] && regnum
!= AVR_SP_REGNUM
)
857 saddr
= avr_make_saddr (get_frame_saved_regs (frame
)[regnum
]);
858 write_register (regnum
,
859 read_memory_unsigned_integer (saddr
, 1));
861 else if (get_frame_saved_regs (frame
)[regnum
] && regnum
== AVR_SP_REGNUM
)
862 write_register (regnum
, get_frame_base (frame
) + 2);
865 /* Don't forget the update the PC too! */
866 write_pc (get_frame_extra_info (frame
)->return_pc
);
868 flush_cached_frames ();
871 /* Return the saved PC from this frame. */
874 avr_frame_saved_pc (struct frame_info
*frame
)
876 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame
),
877 get_frame_base (frame
),
878 get_frame_base (frame
)))
879 return deprecated_read_register_dummy (get_frame_pc (frame
),
880 get_frame_base (frame
),
883 return get_frame_extra_info (frame
)->return_pc
;
887 avr_saved_pc_after_call (struct frame_info
*frame
)
889 unsigned char m1
, m2
;
890 unsigned int sp
= read_register (AVR_SP_REGNUM
);
891 m1
= read_memory_unsigned_integer (avr_make_saddr (sp
+ 1), 1);
892 m2
= read_memory_unsigned_integer (avr_make_saddr (sp
+ 2), 1);
893 return (m2
| (m1
<< 8)) * 2;
896 /* Returns the return address for a dummy. */
899 avr_call_dummy_address (void)
901 return entry_point_address ();
904 /* Setup the return address for a dummy frame, as called by
905 call_function_by_hand. Only necessary when you are using an empty
909 avr_push_return_address (CORE_ADDR pc
, CORE_ADDR sp
)
911 unsigned char buf
[2];
914 struct minimal_symbol
*msymbol
;
921 write_memory (sp
+ 1, buf
, 2);
924 /* FIXME: TRoth/2002-02-18: This should probably be removed since it's a
925 left-over from Denis' original patch which used avr-mon for the target
926 instead of the generic remote target. */
927 if ((strcmp (target_shortname
, "avr-mon") == 0)
928 && (msymbol
= lookup_minimal_symbol ("gdb_break", NULL
, NULL
)))
930 mon_brk
= SYMBOL_VALUE_ADDRESS (msymbol
);
931 store_unsigned_integer (buf
, wordsize
, mon_brk
/ 2);
933 write_memory (sp
+ 1, buf
+ 1, 1);
934 write_memory (sp
+ 2, buf
, 1);
941 avr_skip_prologue (CORE_ADDR pc
)
943 CORE_ADDR func_addr
, func_end
;
944 struct symtab_and_line sal
;
946 /* See what the symbol table says */
948 if (find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
950 sal
= find_pc_line (func_addr
, 0);
952 /* troth/2002-08-05: For some very simple functions, gcc doesn't
953 generate a prologue and the sal.end ends up being the 2-byte ``ret''
954 instruction at the end of the function, but func_end ends up being
955 the address of the first instruction of the _next_ function. By
956 adjusting func_end by 2 bytes, we can catch these functions and not
957 return sal.end if it is the ``ret'' instruction. */
959 if (sal
.line
!= 0 && sal
.end
< (func_end
-2))
963 /* Either we didn't find the start of this function (nothing we can do),
964 or there's no line info, or the line after the prologue is after
965 the end of the function (there probably isn't a prologue). */
971 avr_frame_address (struct frame_info
*fi
)
973 return avr_make_saddr (get_frame_base (fi
));
976 /* Given a GDB frame, determine the address of the calling function's
977 frame. This will be used to create a new GDB frame struct, and
978 then DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC
979 will be called for the new frame.
981 For us, the frame address is its stack pointer value, so we look up
982 the function prologue to determine the caller's sp value, and return it. */
985 avr_frame_chain (struct frame_info
*frame
)
987 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame
),
988 get_frame_base (frame
),
989 get_frame_base (frame
)))
991 /* initialize the return_pc now */
992 get_frame_extra_info (frame
)->return_pc
993 = deprecated_read_register_dummy (get_frame_pc (frame
),
994 get_frame_base (frame
),
996 return get_frame_base (frame
);
998 return (get_frame_extra_info (frame
)->is_main
? 0
999 : get_frame_base (frame
) + get_frame_extra_info (frame
)->framesize
+ 2 /* ret addr */ );
1002 /* Store the address of the place in which to copy the structure the
1003 subroutine will return. This is called from call_function.
1005 We store structs through a pointer passed in the first Argument
1009 avr_store_struct_return (CORE_ADDR addr
, CORE_ADDR sp
)
1011 write_register (0, addr
);
1014 /* Setup the function arguments for calling a function in the inferior.
1016 On the AVR architecture, there are 18 registers (R25 to R8) which are
1017 dedicated for passing function arguments. Up to the first 18 arguments
1018 (depending on size) may go into these registers. The rest go on the stack.
1020 Arguments that are larger than WORDSIZE bytes will be split between two or
1021 more registers as available, but will NOT be split between a register and
1024 An exceptional case exists for struct arguments (and possibly other
1025 aggregates such as arrays) -- if the size is larger than WORDSIZE bytes but
1026 not a multiple of WORDSIZE bytes. In this case the argument is never split
1027 between the registers and the stack, but instead is copied in its entirety
1028 onto the stack, AND also copied into as many registers as there is room
1029 for. In other words, space in registers permitting, two copies of the same
1030 argument are passed in. As far as I can tell, only the one on the stack is
1031 used, although that may be a function of the level of compiler
1032 optimization. I suspect this is a compiler bug. Arguments of these odd
1033 sizes are left-justified within the word (as opposed to arguments smaller
1034 than WORDSIZE bytes, which are right-justified).
1036 If the function is to return an aggregate type such as a struct, the caller
1037 must allocate space into which the callee will copy the return value. In
1038 this case, a pointer to the return value location is passed into the callee
1039 in register R0, which displaces one of the other arguments passed in via
1040 registers R0 to R2. */
1043 avr_push_arguments (int nargs
, struct value
**args
, CORE_ADDR sp
,
1044 int struct_return
, CORE_ADDR struct_addr
)
1046 int stack_alloc
, stack_offset
;
1058 /* Now make sure there's space on the stack */
1059 for (argnum
= 0, stack_alloc
= 0; argnum
< nargs
; argnum
++)
1060 stack_alloc
+= TYPE_LENGTH (VALUE_TYPE (args
[argnum
]));
1061 sp
-= stack_alloc
; /* make room on stack for args */
1062 /* we may over-allocate a little here, but that won't hurt anything */
1065 if (struct_return
) /* "struct return" pointer takes up one argreg */
1067 write_register (--argreg
, struct_addr
);
1070 /* Now load as many as possible of the first arguments into registers, and
1071 push the rest onto the stack. There are 3N bytes in three registers
1072 available. Loop thru args from first to last. */
1074 for (argnum
= 0, stack_offset
= 0; argnum
< nargs
; argnum
++)
1076 type
= VALUE_TYPE (args
[argnum
]);
1077 len
= TYPE_LENGTH (type
);
1078 val
= (char *) VALUE_CONTENTS (args
[argnum
]);
1080 /* NOTE WELL!!!!! This is not an "else if" clause!!! That's because
1081 some *&^%$ things get passed on the stack AND in the registers! */
1083 { /* there's room in registers */
1085 regval
= extract_address (val
+ len
, wordsize
);
1086 write_register (argreg
--, regval
);
1092 /* Initialize the gdbarch structure for the AVR's. */
1094 static struct gdbarch
*
1095 avr_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1097 /* FIXME: TRoth/2002-02-18: I have no idea if avr_call_dummy_words[] should
1098 be bigger or not. Initial testing seems to show that `call my_func()`
1099 works and backtrace from a breakpoint within the call looks correct.
1100 Admittedly, I haven't tested with more than a very simple program. */
1101 static LONGEST avr_call_dummy_words
[] = { 0 };
1103 struct gdbarch
*gdbarch
;
1104 struct gdbarch_tdep
*tdep
;
1106 /* Find a candidate among the list of pre-declared architectures. */
1107 arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1109 return arches
->gdbarch
;
1111 /* None found, create a new architecture from the information provided. */
1112 tdep
= XMALLOC (struct gdbarch_tdep
);
1113 gdbarch
= gdbarch_alloc (&info
, tdep
);
1115 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1116 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1117 set_gdbarch_deprecated_init_frame_pc (gdbarch
, init_frame_pc_default
);
1119 /* If we ever need to differentiate the device types, do it here. */
1120 switch (info
.bfd_arch_info
->mach
)
1130 set_gdbarch_short_bit (gdbarch
, 2 * TARGET_CHAR_BIT
);
1131 set_gdbarch_int_bit (gdbarch
, 2 * TARGET_CHAR_BIT
);
1132 set_gdbarch_long_bit (gdbarch
, 4 * TARGET_CHAR_BIT
);
1133 set_gdbarch_long_long_bit (gdbarch
, 8 * TARGET_CHAR_BIT
);
1134 set_gdbarch_ptr_bit (gdbarch
, 2 * TARGET_CHAR_BIT
);
1135 set_gdbarch_addr_bit (gdbarch
, 32);
1136 set_gdbarch_bfd_vma_bit (gdbarch
, 32); /* FIXME: TRoth/2002-02-18: Is this needed? */
1138 set_gdbarch_float_bit (gdbarch
, 4 * TARGET_CHAR_BIT
);
1139 set_gdbarch_double_bit (gdbarch
, 4 * TARGET_CHAR_BIT
);
1140 set_gdbarch_long_double_bit (gdbarch
, 4 * TARGET_CHAR_BIT
);
1142 set_gdbarch_float_format (gdbarch
, &floatformat_ieee_single_little
);
1143 set_gdbarch_double_format (gdbarch
, &floatformat_ieee_single_little
);
1144 set_gdbarch_long_double_format (gdbarch
, &floatformat_ieee_single_little
);
1146 set_gdbarch_read_pc (gdbarch
, avr_read_pc
);
1147 set_gdbarch_write_pc (gdbarch
, avr_write_pc
);
1148 set_gdbarch_read_fp (gdbarch
, avr_read_fp
);
1149 set_gdbarch_read_sp (gdbarch
, avr_read_sp
);
1150 set_gdbarch_deprecated_dummy_write_sp (gdbarch
, avr_write_sp
);
1152 set_gdbarch_num_regs (gdbarch
, AVR_NUM_REGS
);
1154 set_gdbarch_sp_regnum (gdbarch
, AVR_SP_REGNUM
);
1155 set_gdbarch_fp_regnum (gdbarch
, AVR_FP_REGNUM
);
1156 set_gdbarch_pc_regnum (gdbarch
, AVR_PC_REGNUM
);
1158 set_gdbarch_register_name (gdbarch
, avr_register_name
);
1159 set_gdbarch_register_size (gdbarch
, 1);
1160 set_gdbarch_register_bytes (gdbarch
, AVR_NUM_REG_BYTES
);
1161 set_gdbarch_register_byte (gdbarch
, avr_register_byte
);
1162 set_gdbarch_register_raw_size (gdbarch
, avr_register_raw_size
);
1163 set_gdbarch_deprecated_max_register_raw_size (gdbarch
, 4);
1164 set_gdbarch_register_virtual_size (gdbarch
, avr_register_virtual_size
);
1165 set_gdbarch_deprecated_max_register_virtual_size (gdbarch
, 4);
1166 set_gdbarch_register_virtual_type (gdbarch
, avr_register_virtual_type
);
1168 set_gdbarch_print_insn (gdbarch
, print_insn_avr
);
1170 set_gdbarch_call_dummy_address (gdbarch
, avr_call_dummy_address
);
1171 set_gdbarch_call_dummy_words (gdbarch
, avr_call_dummy_words
);
1173 /* set_gdbarch_believe_pcc_promotion (gdbarch, 1); // TRoth: should this be set? */
1175 set_gdbarch_address_to_pointer (gdbarch
, avr_address_to_pointer
);
1176 set_gdbarch_pointer_to_address (gdbarch
, avr_pointer_to_address
);
1177 set_gdbarch_deprecated_push_arguments (gdbarch
, avr_push_arguments
);
1178 set_gdbarch_deprecated_push_return_address (gdbarch
, avr_push_return_address
);
1179 set_gdbarch_deprecated_pop_frame (gdbarch
, avr_pop_frame
);
1181 set_gdbarch_use_struct_convention (gdbarch
, generic_use_struct_convention
);
1182 set_gdbarch_deprecated_store_struct_return (gdbarch
, avr_store_struct_return
);
1184 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch
, avr_scan_prologue
);
1185 set_gdbarch_deprecated_init_extra_frame_info (gdbarch
, avr_init_extra_frame_info
);
1186 set_gdbarch_skip_prologue (gdbarch
, avr_skip_prologue
);
1187 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1189 set_gdbarch_decr_pc_after_break (gdbarch
, 0);
1191 set_gdbarch_function_start_offset (gdbarch
, 0);
1192 set_gdbarch_remote_translate_xfer_address (gdbarch
,
1193 avr_remote_translate_xfer_address
);
1194 set_gdbarch_frame_args_skip (gdbarch
, 0);
1195 set_gdbarch_frameless_function_invocation (gdbarch
, frameless_look_for_prologue
); /* ??? */
1196 set_gdbarch_deprecated_frame_chain (gdbarch
, avr_frame_chain
);
1197 set_gdbarch_deprecated_frame_saved_pc (gdbarch
, avr_frame_saved_pc
);
1198 set_gdbarch_frame_args_address (gdbarch
, avr_frame_address
);
1199 set_gdbarch_frame_locals_address (gdbarch
, avr_frame_address
);
1200 set_gdbarch_deprecated_saved_pc_after_call (gdbarch
, avr_saved_pc_after_call
);
1201 set_gdbarch_frame_num_args (gdbarch
, frame_num_args_unknown
);
1203 set_gdbarch_convert_from_func_ptr_addr (gdbarch
,
1204 avr_convert_from_func_ptr_addr
);
1209 /* Send a query request to the avr remote target asking for values of the io
1210 registers. If args parameter is not NULL, then the user has requested info
1211 on a specific io register [This still needs implemented and is ignored for
1212 now]. The query string should be one of these forms:
1214 "Ravr.io_reg" -> reply is "NN" number of io registers
1216 "Ravr.io_reg:addr,len" where addr is first register and len is number of
1217 registers to be read. The reply should be "<NAME>,VV;" for each io register
1218 where, <NAME> is a string, and VV is the hex value of the register.
1220 All io registers are 8-bit. */
1223 avr_io_reg_read_command (char *args
, int from_tty
)
1229 unsigned int nreg
= 0;
1233 /* fprintf_unfiltered (gdb_stderr, "DEBUG: avr_io_reg_read_command (\"%s\", %d)\n", */
1234 /* args, from_tty); */
1236 if (!current_target
.to_query
)
1238 fprintf_unfiltered (gdb_stderr
,
1239 "ERR: info io_registers NOT supported by current target\n");
1243 /* Just get the maximum buffer size. */
1244 target_query ((int) 'R', 0, 0, &bufsiz
);
1245 if (bufsiz
> sizeof (buf
))
1246 bufsiz
= sizeof (buf
);
1248 /* Find out how many io registers the target has. */
1249 strcpy (query
, "avr.io_reg");
1250 target_query ((int) 'R', query
, buf
, &bufsiz
);
1252 if (strncmp (buf
, "", bufsiz
) == 0)
1254 fprintf_unfiltered (gdb_stderr
,
1255 "info io_registers NOT supported by target\n");
1259 if (sscanf (buf
, "%x", &nreg
) != 1)
1261 fprintf_unfiltered (gdb_stderr
,
1262 "Error fetching number of io registers\n");
1266 reinitialize_more_filter ();
1268 printf_unfiltered ("Target has %u io registers:\n\n", nreg
);
1270 /* only fetch up to 8 registers at a time to keep the buffer small */
1273 for (i
= 0; i
< nreg
; i
+= step
)
1275 /* how many registers this round? */
1278 j
= nreg
- i
; /* last block is less than 8 registers */
1280 snprintf (query
, sizeof (query
) - 1, "avr.io_reg:%x,%x", i
, j
);
1281 target_query ((int) 'R', query
, buf
, &bufsiz
);
1284 for (k
= i
; k
< (i
+ j
); k
++)
1286 if (sscanf (p
, "%[^,],%x;", query
, &val
) == 2)
1288 printf_filtered ("[%02x] %-15s : %02x\n", k
, query
, val
);
1289 while ((*p
!= ';') && (*p
!= '\0'))
1291 p
++; /* skip over ';' */
1300 _initialize_avr_tdep (void)
1302 register_gdbarch_init (bfd_arch_avr
, avr_gdbarch_init
);
1304 /* Add a new command to allow the user to query the avr remote target for
1305 the values of the io space registers in a saner way than just using
1308 /* FIXME: TRoth/2002-02-18: This should probably be changed to 'info avr
1309 io_registers' to signify it is not available on other platforms. */
1311 add_cmd ("io_registers", class_info
, avr_io_reg_read_command
,
1312 "query remote avr target for io space register values", &infolist
);