1 /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
3 Copyright (C) 2003, 2005, 2006, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "solib-svr4.h"
31 #include "floatformat.h"
33 #include "reggroups.h"
36 #include "dummy-frame.h"
37 #include "elf/dwarf2.h"
38 #include "dwarf2-frame.h"
39 #include "dwarf2loc.h"
41 #include "frame-base.h"
42 #include "frame-unwind.h"
44 #include "arch-utils.h"
51 #include "gdb_assert.h"
53 #include "xtensa-isa.h"
54 #include "xtensa-tdep.h"
55 #include "xtensa-config.h"
58 static int xtensa_debug_level = 0;
60 #define DEBUGWARN(args...) \
61 if (xtensa_debug_level > 0) \
62 fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
64 #define DEBUGINFO(args...) \
65 if (xtensa_debug_level > 1) \
66 fprintf_unfiltered (gdb_stdlog, "(info ) " args)
68 #define DEBUGTRACE(args...) \
69 if (xtensa_debug_level > 2) \
70 fprintf_unfiltered (gdb_stdlog, "(trace) " args)
72 #define DEBUGVERB(args...) \
73 if (xtensa_debug_level > 3) \
74 fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
77 /* According to the ABI, the SP must be aligned to 16-byte boundaries. */
78 #define SP_ALIGNMENT 16
81 /* On Windowed ABI, we use a6 through a11 for passing arguments
82 to a function called by GDB because CALL4 is used. */
83 #define ARGS_NUM_REGS 6
84 #define REGISTER_SIZE 4
87 /* Extract the call size from the return address or PS register. */
88 #define PS_CALLINC_SHIFT 16
89 #define PS_CALLINC_MASK 0x00030000
90 #define CALLINC(ps) (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
91 #define WINSIZE(ra) (4 * (( (ra) >> 30) & 0x3))
93 /* ABI-independent macros. */
94 #define ARG_NOF(gdbarch) \
95 (gdbarch_tdep (gdbarch)->call_abi \
96 == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
97 #define ARG_1ST(gdbarch) \
98 (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only \
99 ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
100 : (gdbarch_tdep (gdbarch)->a0_base + 6))
102 /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
103 indicates that the instruction is an ENTRY instruction. */
105 #define XTENSA_IS_ENTRY(gdbarch, op1) \
106 ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
107 ? ((op1) == 0x6c) : ((op1) == 0x36))
109 #define XTENSA_ENTRY_LENGTH 3
111 /* windowing_enabled() returns true, if windowing is enabled.
112 WOE must be set to 1; EXCM to 0.
113 Note: We assume that EXCM is always 0 for XEA1. */
115 #define PS_WOE (1<<18)
116 #define PS_EXC (1<<4)
118 /* Convert a live A-register number to the corresponding AR-register number. */
120 arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
122 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
125 arreg = a_regnum - tdep->a0_base;
126 arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
127 arreg &= tdep->num_aregs - 1;
129 return arreg + tdep->ar_base;
132 /* Convert a live AR-register number to the corresponding A-register order
133 number in a range [0..15]. Return -1, if AR_REGNUM is out of WB window. */
135 areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
137 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
140 areg = ar_regnum - tdep->ar_base;
141 if (areg < 0 || areg >= tdep->num_aregs)
143 areg = (areg - wb * 4) & (tdep->num_aregs - 1);
144 return (areg > 15) ? -1 : areg;
148 windowing_enabled (CORE_ADDR ps)
150 return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
153 /* Return the window size of the previous call to the function from which we
156 This function is used to extract the return value after a called function
157 has returned to the caller. On Xtensa, the register that holds the return
158 value (from the perspective of the caller) depends on what call
159 instruction was used. For now, we are assuming that the call instruction
160 precedes the current address, so we simply analyze the call instruction.
161 If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
162 method to call the inferior function. */
165 extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
171 DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
173 /* Read the previous instruction (should be a call[x]{4|8|12}. */
174 read_memory (pc-3, buf, 3);
175 insn = extract_unsigned_integer (buf, 3);
177 /* Decode call instruction:
179 call{0,4,8,12} OFFSET || {00,01,10,11} || 0101
180 callx{0,4,8,12} OFFSET || 11 || {00,01,10,11} || 0000
182 call{0,4,8,12} 0101 || {00,01,10,11} || OFFSET
183 callx{0,4,8,12} 0000 || {00,01,10,11} || 11 || OFFSET. */
185 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
187 if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
188 winsize = (insn & 0x30) >> 2; /* 0, 4, 8, 12. */
192 if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
193 winsize = (insn >> 16) & 0xc; /* 0, 4, 8, 12. */
199 /* REGISTER INFORMATION */
201 /* Returns the name of a register. */
203 xtensa_register_name (struct gdbarch *gdbarch, int regnum)
205 /* Return the name stored in the register map. */
206 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
207 + gdbarch_num_pseudo_regs (gdbarch))
208 return gdbarch_tdep (gdbarch)->regmap[regnum].name;
210 internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
214 /* Return the type of a register. Create a new type, if necessary. */
216 static struct ctype_cache
218 struct ctype_cache *next;
220 struct type *virtual_type;
221 } *type_entries = NULL;
224 xtensa_register_type (struct gdbarch *gdbarch, int regnum)
226 /* Return signed integer for ARx and Ax registers. */
227 if ((regnum >= gdbarch_tdep (gdbarch)->ar_base
228 && regnum < gdbarch_tdep (gdbarch)->ar_base
229 + gdbarch_tdep (gdbarch)->num_aregs)
230 || (regnum >= gdbarch_tdep (gdbarch)->a0_base
231 && regnum < gdbarch_tdep (gdbarch)->a0_base + 16))
232 return builtin_type (gdbarch)->builtin_int;
234 if (regnum == gdbarch_pc_regnum (gdbarch)
235 || regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
236 return builtin_type (gdbarch)->builtin_data_ptr;
238 /* Return the stored type for all other registers. */
239 else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
240 + gdbarch_num_pseudo_regs (gdbarch))
242 xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
244 /* Set ctype for this register (only the first time). */
248 struct ctype_cache *tp;
249 int size = reg->byte_size;
251 /* We always use the memory representation,
252 even if the register width is smaller. */
256 reg->ctype = builtin_type_uint8;
260 reg->ctype = builtin_type_uint16;
264 reg->ctype = builtin_type_uint32;
268 reg->ctype = builtin_type_uint64;
272 reg->ctype = builtin_type_uint128;
276 for (tp = type_entries; tp != NULL; tp = tp->next)
277 if (tp->size == size)
282 char *name = xmalloc (16);
283 tp = xmalloc (sizeof (struct ctype_cache));
284 tp->next = type_entries;
288 sprintf (name, "int%d", size * 8);
289 tp->virtual_type = init_type (TYPE_CODE_INT, size,
290 TYPE_FLAG_UNSIGNED, name,
294 reg->ctype = tp->virtual_type;
300 internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
305 /* Return the 'local' register number for stubs, dwarf2, etc.
306 The debugging information enumerates registers starting from 0 for A0
307 to n for An. So, we only have to add the base number for A0. */
310 xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
314 if (regnum >= 0 && regnum < 16)
315 return gdbarch_tdep (gdbarch)->a0_base + regnum;
318 i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
320 if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
323 internal_error (__FILE__, __LINE__,
324 _("invalid dwarf/stabs register number %d"), regnum);
329 /* Write the bits of a masked register to the various registers.
330 Only the masked areas of these registers are modified; the other
331 fields are untouched. The size of masked registers is always less
332 than or equal to 32 bits. */
335 xtensa_register_write_masked (struct regcache *regcache,
336 xtensa_register_t *reg, const gdb_byte *buffer)
338 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
339 const xtensa_mask_t *mask = reg->mask;
341 int shift = 0; /* Shift for next mask (mod 32). */
342 int start, size; /* Start bit and size of current mask. */
344 unsigned int *ptr = value;
345 unsigned int regval, m, mem = 0;
347 int bytesize = reg->byte_size;
348 int bitsize = bytesize * 8;
351 DEBUGTRACE ("xtensa_register_write_masked ()\n");
353 /* Copy the masked register to host byte-order. */
354 if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
355 for (i = 0; i < bytesize; i++)
358 mem |= (buffer[bytesize - i - 1] << 24);
363 for (i = 0; i < bytesize; i++)
366 mem |= (buffer[i] << 24);
371 /* We might have to shift the final value:
372 bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
373 bytesize & 3 == x -> shift (4-x) * 8. */
375 *ptr = mem >> (((0 - bytesize) & 3) * 8);
379 /* Write the bits to the masked areas of the other registers. */
380 for (i = 0; i < mask->count; i++)
382 start = mask->mask[i].bit_start;
383 size = mask->mask[i].bit_size;
384 regval = mem >> shift;
386 if ((shift += size) > bitsize)
387 error (_("size of all masks is larger than the register"));
396 regval |= mem << (size - shift);
399 /* Make sure we have a valid register. */
400 r = mask->mask[i].reg_num;
401 if (r >= 0 && size > 0)
403 /* Don't overwrite the unmasked areas. */
405 regcache_cooked_read_unsigned (regcache, r, &old_val);
406 m = 0xffffffff >> (32 - size) << start;
408 regval = (regval & m) | (old_val & ~m);
409 regcache_cooked_write_unsigned (regcache, r, regval);
415 /* Read a tie state or mapped registers. Read the masked areas
416 of the registers and assemble them into a single value. */
419 xtensa_register_read_masked (struct regcache *regcache,
420 xtensa_register_t *reg, gdb_byte *buffer)
422 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
423 const xtensa_mask_t *mask = reg->mask;
428 unsigned int *ptr = value;
429 unsigned int regval, mem = 0;
431 int bytesize = reg->byte_size;
432 int bitsize = bytesize * 8;
435 DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
436 reg->name == 0 ? "" : reg->name);
438 /* Assemble the register from the masked areas of other registers. */
439 for (i = 0; i < mask->count; i++)
441 int r = mask->mask[i].reg_num;
445 regcache_cooked_read_unsigned (regcache, r, &val);
446 regval = (unsigned int) val;
451 start = mask->mask[i].bit_start;
452 size = mask->mask[i].bit_size;
457 regval &= (0xffffffff >> (32 - size));
459 mem |= regval << shift;
461 if ((shift += size) > bitsize)
462 error (_("size of all masks is larger than the register"));
473 mem = regval >> (size - shift);
480 /* Copy value to target byte order. */
484 if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
485 for (i = 0; i < bytesize; i++)
489 buffer[bytesize - i - 1] = mem & 0xff;
493 for (i = 0; i < bytesize; i++)
497 buffer[i] = mem & 0xff;
503 /* Read pseudo registers. */
506 xtensa_pseudo_register_read (struct gdbarch *gdbarch,
507 struct regcache *regcache,
511 DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
512 regnum, xtensa_register_name (gdbarch, regnum));
514 if (regnum == gdbarch_num_regs (gdbarch)
515 + gdbarch_num_pseudo_regs (gdbarch) - 1)
516 regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
518 /* Read aliases a0..a15, if this is a Windowed ABI. */
519 if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
520 && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
521 && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
523 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
525 regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
526 regnum = arreg_number (gdbarch, regnum,
527 extract_unsigned_integer (buf, 4));
530 /* We can always read non-pseudo registers. */
531 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
532 regcache_raw_read (regcache, regnum, buffer);
535 /* We have to find out how to deal with priveleged registers.
536 Let's treat them as pseudo-registers, but we cannot read/write them. */
538 else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
540 buffer[0] = (gdb_byte)0;
541 buffer[1] = (gdb_byte)0;
542 buffer[2] = (gdb_byte)0;
543 buffer[3] = (gdb_byte)0;
545 /* Pseudo registers. */
547 && regnum < gdbarch_num_regs (gdbarch)
548 + gdbarch_num_pseudo_regs (gdbarch))
550 xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
551 xtensa_register_type_t type = reg->type;
552 int flags = gdbarch_tdep (gdbarch)->target_flags;
554 /* We cannot read Unknown or Unmapped registers. */
555 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
557 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
559 warning (_("cannot read register %s"),
560 xtensa_register_name (gdbarch, regnum));
565 /* Some targets cannot read TIE register files. */
566 else if (type == xtRegisterTypeTieRegfile)
568 /* Use 'fetch' to get register? */
569 if (flags & xtTargetFlagsUseFetchStore)
571 warning (_("cannot read register"));
575 /* On some targets (esp. simulators), we can always read the reg. */
576 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
578 warning (_("cannot read register"));
583 /* We can always read mapped registers. */
584 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
586 xtensa_register_read_masked (regcache, reg, buffer);
590 /* Assume that we can read the register. */
591 regcache_raw_read (regcache, regnum, buffer);
594 internal_error (__FILE__, __LINE__,
595 _("invalid register number %d"), regnum);
599 /* Write pseudo registers. */
602 xtensa_pseudo_register_write (struct gdbarch *gdbarch,
603 struct regcache *regcache,
605 const gdb_byte *buffer)
607 DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
608 regnum, xtensa_register_name (gdbarch, regnum));
610 if (regnum == gdbarch_num_regs (gdbarch)
611 + gdbarch_num_pseudo_regs (gdbarch) -1)
612 regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
614 /* Renumber register, if aliase a0..a15 on Windowed ABI. */
615 if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
616 && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
617 && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
619 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
622 regcache_raw_read (regcache,
623 gdbarch_tdep (gdbarch)->wb_regnum, buf);
624 regnum = arreg_number (gdbarch, regnum,
625 extract_unsigned_integer (buf, 4));
628 /* We can always write 'core' registers.
629 Note: We might have converted Ax->ARy. */
630 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
631 regcache_raw_write (regcache, regnum, buffer);
633 /* We have to find out how to deal with priveleged registers.
634 Let's treat them as pseudo-registers, but we cannot read/write them. */
636 else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
640 /* Pseudo registers. */
642 && regnum < gdbarch_num_regs (gdbarch)
643 + gdbarch_num_pseudo_regs (gdbarch))
645 xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
646 xtensa_register_type_t type = reg->type;
647 int flags = gdbarch_tdep (gdbarch)->target_flags;
649 /* On most targets, we cannot write registers
650 of type "Unknown" or "Unmapped". */
651 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
653 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
655 warning (_("cannot write register %s"),
656 xtensa_register_name (gdbarch, regnum));
661 /* Some targets cannot read TIE register files. */
662 else if (type == xtRegisterTypeTieRegfile)
664 /* Use 'store' to get register? */
665 if (flags & xtTargetFlagsUseFetchStore)
667 warning (_("cannot write register"));
671 /* On some targets (esp. simulators), we can always write
673 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
675 warning (_("cannot write register"));
680 /* We can always write mapped registers. */
681 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
683 xtensa_register_write_masked (regcache, reg, buffer);
687 /* Assume that we can write the register. */
688 regcache_raw_write (regcache, regnum, buffer);
691 internal_error (__FILE__, __LINE__,
692 _("invalid register number %d"), regnum);
695 static struct reggroup *xtensa_ar_reggroup;
696 static struct reggroup *xtensa_user_reggroup;
697 static struct reggroup *xtensa_vectra_reggroup;
698 static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
701 xtensa_init_reggroups (void)
703 xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
704 xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
705 xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
707 xtensa_cp[0] = reggroup_new ("cp0", USER_REGGROUP);
708 xtensa_cp[1] = reggroup_new ("cp1", USER_REGGROUP);
709 xtensa_cp[2] = reggroup_new ("cp2", USER_REGGROUP);
710 xtensa_cp[3] = reggroup_new ("cp3", USER_REGGROUP);
711 xtensa_cp[4] = reggroup_new ("cp4", USER_REGGROUP);
712 xtensa_cp[5] = reggroup_new ("cp5", USER_REGGROUP);
713 xtensa_cp[6] = reggroup_new ("cp6", USER_REGGROUP);
714 xtensa_cp[7] = reggroup_new ("cp7", USER_REGGROUP);
718 xtensa_add_reggroups (struct gdbarch *gdbarch)
722 /* Predefined groups. */
723 reggroup_add (gdbarch, all_reggroup);
724 reggroup_add (gdbarch, save_reggroup);
725 reggroup_add (gdbarch, restore_reggroup);
726 reggroup_add (gdbarch, system_reggroup);
727 reggroup_add (gdbarch, vector_reggroup);
728 reggroup_add (gdbarch, general_reggroup);
729 reggroup_add (gdbarch, float_reggroup);
731 /* Xtensa-specific groups. */
732 reggroup_add (gdbarch, xtensa_ar_reggroup);
733 reggroup_add (gdbarch, xtensa_user_reggroup);
734 reggroup_add (gdbarch, xtensa_vectra_reggroup);
736 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
737 reggroup_add (gdbarch, xtensa_cp[i]);
741 xtensa_coprocessor_register_group (struct reggroup *group)
745 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
746 if (group == xtensa_cp[i])
752 #define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
753 | XTENSA_REGISTER_FLAGS_WRITABLE \
754 | XTENSA_REGISTER_FLAGS_VOLATILE)
756 #define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
757 | XTENSA_REGISTER_FLAGS_WRITABLE)
760 xtensa_register_reggroup_p (struct gdbarch *gdbarch,
762 struct reggroup *group)
764 xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
765 xtensa_register_type_t type = reg->type;
766 xtensa_register_group_t rg = reg->group;
769 /* First, skip registers that are not visible to this target
770 (unknown and unmapped registers when not using ISS). */
772 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
774 if (group == all_reggroup)
776 if (group == xtensa_ar_reggroup)
777 return rg & xtRegisterGroupAddrReg;
778 if (group == xtensa_user_reggroup)
779 return rg & xtRegisterGroupUser;
780 if (group == float_reggroup)
781 return rg & xtRegisterGroupFloat;
782 if (group == general_reggroup)
783 return rg & xtRegisterGroupGeneral;
784 if (group == float_reggroup)
785 return rg & xtRegisterGroupFloat;
786 if (group == system_reggroup)
787 return rg & xtRegisterGroupState;
788 if (group == vector_reggroup || group == xtensa_vectra_reggroup)
789 return rg & xtRegisterGroupVectra;
790 if (group == save_reggroup || group == restore_reggroup)
791 return (regnum < gdbarch_num_regs (gdbarch)
792 && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
793 if ((cp_number = xtensa_coprocessor_register_group (group)) >= 0)
794 return rg & (xtRegisterGroupCP0 << cp_number);
800 /* Supply register REGNUM from the buffer specified by GREGS and LEN
801 in the general-purpose register set REGSET to register cache
802 REGCACHE. If REGNUM is -1 do this for all registers in REGSET. */
805 xtensa_supply_gregset (const struct regset *regset,
811 const xtensa_elf_gregset_t *regs = gregs;
812 struct gdbarch *gdbarch = get_regcache_arch (rc);
815 DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...) \n", regnum);
817 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
818 regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) ®s->pc);
819 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
820 regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) ®s->ps);
821 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
822 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
823 (char *) ®s->windowbase);
824 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
825 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
826 (char *) ®s->windowstart);
827 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
828 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
829 (char *) ®s->lbeg);
830 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
831 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
832 (char *) ®s->lend);
833 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
834 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
835 (char *) ®s->lcount);
836 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
837 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
838 (char *) ®s->sar);
839 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
840 && regnum < gdbarch_tdep (gdbarch)->ar_base
841 + gdbarch_tdep (gdbarch)->num_aregs)
842 regcache_raw_supply (rc, regnum,
843 (char *) ®s->ar[regnum - gdbarch_tdep
844 (gdbarch)->ar_base]);
845 else if (regnum == -1)
847 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
848 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
849 (char *) ®s->ar[i]);
854 /* Xtensa register set. */
860 xtensa_supply_gregset
864 /* Return the appropriate register set for the core
865 section identified by SECT_NAME and SECT_SIZE. */
867 static const struct regset *
868 xtensa_regset_from_core_section (struct gdbarch *core_arch,
869 const char *sect_name,
872 DEBUGTRACE ("xtensa_regset_from_core_section "
873 "(..., sect_name==\"%s\", sect_size==%x) \n",
874 sect_name, (unsigned int) sect_size);
876 if (strcmp (sect_name, ".reg") == 0
877 && sect_size >= sizeof(xtensa_elf_gregset_t))
878 return &xtensa_gregset;
884 /* Handling frames. */
886 /* Number of registers to save in case of Windowed ABI. */
887 #define XTENSA_NUM_SAVED_AREGS 12
889 /* Frame cache part for Windowed ABI. */
890 typedef struct xtensa_windowed_frame_cache
892 int wb; /* WINDOWBASE of the previous frame. */
893 int callsize; /* Call size of this frame. */
894 int ws; /* WINDOWSTART of the previous frame. It keeps track of
895 life windows only. If there is no bit set for the
896 window, that means it had been already spilled
897 because of window overflow. */
899 /* Spilled A-registers from the previous frame.
900 AREGS[i] == -1, if corresponding AR is alive. */
901 CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
902 } xtensa_windowed_frame_cache_t;
904 /* Call0 ABI Definitions. */
906 #define C0_MAXOPDS 3 /* Maximum number of operands for prologue analysis. */
907 #define C0_NREGS 16 /* Number of A-registers to track. */
908 #define C0_CLESV 12 /* Callee-saved registers are here and up. */
909 #define C0_SP 1 /* Register used as SP. */
910 #define C0_FP 15 /* Register used as FP. */
911 #define C0_RA 0 /* Register used as return address. */
912 #define C0_ARGS 2 /* Register used as first arg/retval. */
913 #define C0_NARGS 6 /* Number of A-regs for args/retvals. */
915 /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
916 A-register where the current content of the reg came from (in terms
917 of an original reg and a constant). Negative values of c0_rt[n].fp_reg
918 mean that the orignal content of the register was saved to the stack.
919 c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
920 know where SP will end up until the entire prologue has been analyzed. */
922 #define C0_CONST -1 /* fr_reg value if register contains a constant. */
923 #define C0_INEXP -2 /* fr_reg value if inexpressible as reg + offset. */
924 #define C0_NOSTK -1 /* to_stk value if register has not been stored. */
926 extern xtensa_isa xtensa_default_isa;
928 typedef struct xtensa_c0reg
930 int fr_reg; /* original register from which register content
931 is derived, or C0_CONST, or C0_INEXP. */
932 int fr_ofs; /* constant offset from reg, or immediate value. */
933 int to_stk; /* offset from original SP to register (4-byte aligned),
934 or C0_NOSTK if register has not been saved. */
938 /* Frame cache part for Call0 ABI. */
939 typedef struct xtensa_call0_frame_cache
941 int c0_frmsz; /* Stack frame size. */
942 int c0_hasfp; /* Current frame uses frame pointer. */
943 int fp_regnum; /* A-register used as FP. */
944 int c0_fp; /* Actual value of frame pointer. */
945 xtensa_c0reg_t c0_rt[C0_NREGS]; /* Register tracking information. */
946 } xtensa_call0_frame_cache_t;
948 typedef struct xtensa_frame_cache
950 CORE_ADDR base; /* Stack pointer of this frame. */
951 CORE_ADDR pc; /* PC at the entry point to the function. */
952 CORE_ADDR ra; /* The raw return address (without CALLINC). */
953 CORE_ADDR ps; /* The PS register of this frame. */
954 CORE_ADDR prev_sp; /* Stack Pointer of the previous frame. */
955 int call0; /* It's a call0 framework (else windowed). */
958 xtensa_windowed_frame_cache_t wd; /* call0 == false. */
959 xtensa_call0_frame_cache_t c0; /* call0 == true. */
961 } xtensa_frame_cache_t;
964 static struct xtensa_frame_cache *
965 xtensa_alloc_frame_cache (int windowed)
967 xtensa_frame_cache_t *cache;
970 DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
972 cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
979 cache->call0 = !windowed;
982 cache->c0.c0_frmsz = -1;
983 cache->c0.c0_hasfp = 0;
984 cache->c0.fp_regnum = -1;
985 cache->c0.c0_fp = -1;
987 for (i = 0; i < C0_NREGS; i++)
989 cache->c0.c0_rt[i].fr_reg = i;
990 cache->c0.c0_rt[i].fr_ofs = 0;
991 cache->c0.c0_rt[i].to_stk = C0_NOSTK;
998 cache->wd.callsize = -1;
1000 for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
1001 cache->wd.aregs[i] = -1;
1008 xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
1010 return address & ~15;
1015 xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1020 DEBUGTRACE ("xtensa_unwind_pc (next_frame = %p)\n", next_frame);
1022 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1023 pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1025 DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
1031 static struct frame_id
1032 xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1036 /* THIS-FRAME is a dummy frame. Return a frame ID of that frame. */
1038 pc = get_frame_pc (this_frame);
1039 fp = get_frame_register_unsigned
1040 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1042 /* Make dummy frame ID unique by adding a constant. */
1043 return frame_id_build (fp + SP_ALIGNMENT, pc);
1046 /* Returns the best guess about which register is a frame pointer
1047 for the function containing CURRENT_PC. */
1049 #define XTENSA_ISA_BSZ 32 /* Instruction buffer size. */
1052 xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
1054 #define RETURN_FP goto done
1056 unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
1057 CORE_ADDR start_addr;
1059 xtensa_insnbuf ins, slot;
1060 char ibuf[XTENSA_ISA_BSZ];
1061 CORE_ADDR ia, bt, ba;
1063 int ilen, islots, is;
1065 const char *opcname;
1067 find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
1068 if (start_addr == 0)
1071 if (!xtensa_default_isa)
1072 xtensa_default_isa = xtensa_isa_init (0, 0);
1073 isa = xtensa_default_isa;
1074 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
1075 ins = xtensa_insnbuf_alloc (isa);
1076 slot = xtensa_insnbuf_alloc (isa);
1079 for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
1081 if (ia + xtensa_isa_maxlength (isa) > bt)
1084 bt = (ba + XTENSA_ISA_BSZ) < current_pc
1085 ? ba + XTENSA_ISA_BSZ : current_pc;
1086 read_memory (ba, ibuf, bt - ba);
1089 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
1090 ifmt = xtensa_format_decode (isa, ins);
1091 if (ifmt == XTENSA_UNDEFINED)
1093 ilen = xtensa_format_length (isa, ifmt);
1094 if (ilen == XTENSA_UNDEFINED)
1096 islots = xtensa_format_num_slots (isa, ifmt);
1097 if (islots == XTENSA_UNDEFINED)
1100 for (is = 0; is < islots; ++is)
1102 if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
1105 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
1106 if (opc == XTENSA_UNDEFINED)
1109 opcname = xtensa_opcode_name (isa, opc);
1111 if (strcasecmp (opcname, "mov.n") == 0
1112 || strcasecmp (opcname, "or") == 0)
1114 unsigned int register_operand;
1116 /* Possible candidate for setting frame pointer
1117 from A1. This is what we are looking for. */
1119 if (xtensa_operand_get_field (isa, opc, 1, ifmt,
1120 is, slot, ®ister_operand) != 0)
1122 if (xtensa_operand_decode (isa, opc, 1, ®ister_operand) != 0)
1124 if (register_operand == 1) /* Mov{.n} FP A1. */
1126 if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
1127 ®ister_operand) != 0)
1129 if (xtensa_operand_decode (isa, opc, 0,
1130 ®ister_operand) != 0)
1133 fp_regnum = gdbarch_tdep (gdbarch)->a0_base + register_operand;
1139 /* We have problems decoding the memory. */
1141 || strcasecmp (opcname, "ill") == 0
1142 || strcasecmp (opcname, "ill.n") == 0
1143 /* Hit planted breakpoint. */
1144 || strcasecmp (opcname, "break") == 0
1145 || strcasecmp (opcname, "break.n") == 0
1146 /* Flow control instructions finish prologue. */
1147 || xtensa_opcode_is_branch (isa, opc) > 0
1148 || xtensa_opcode_is_jump (isa, opc) > 0
1149 || xtensa_opcode_is_loop (isa, opc) > 0
1150 || xtensa_opcode_is_call (isa, opc) > 0
1151 || strcasecmp (opcname, "simcall") == 0
1152 || strcasecmp (opcname, "syscall") == 0)
1153 /* Can not continue analysis. */
1158 xtensa_insnbuf_free(isa, slot);
1159 xtensa_insnbuf_free(isa, ins);
1163 /* The key values to identify the frame using "cache" are
1165 cache->base = SP (or best guess about FP) of this frame;
1166 cache->pc = entry-PC (entry point of the frame function);
1167 cache->prev_sp = SP of the previous frame.
1171 call0_frame_cache (struct frame_info *this_frame,
1172 xtensa_frame_cache_t *cache,
1173 CORE_ADDR pc, CORE_ADDR litbase);
1175 static struct xtensa_frame_cache *
1176 xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
1178 xtensa_frame_cache_t *cache;
1179 CORE_ADDR ra, wb, ws, pc, sp, ps;
1180 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1181 unsigned int fp_regnum;
1188 ps = get_frame_register_unsigned (this_frame, gdbarch_ps_regnum (gdbarch));
1189 windowed = windowing_enabled (ps);
1191 /* Get pristine xtensa-frame. */
1192 cache = xtensa_alloc_frame_cache (windowed);
1193 *this_cache = cache;
1195 pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
1199 /* Get WINDOWBASE, WINDOWSTART, and PS registers. */
1200 wb = get_frame_register_unsigned (this_frame,
1201 gdbarch_tdep (gdbarch)->wb_regnum);
1202 ws = get_frame_register_unsigned (this_frame,
1203 gdbarch_tdep (gdbarch)->ws_regnum);
1205 op1 = read_memory_integer (pc, 1);
1206 if (XTENSA_IS_ENTRY (gdbarch, op1))
1208 int callinc = CALLINC (ps);
1209 ra = get_frame_register_unsigned
1210 (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
1212 /* ENTRY hasn't been executed yet, therefore callsize is still 0. */
1213 cache->wd.callsize = 0;
1216 cache->prev_sp = get_frame_register_unsigned
1217 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1219 /* This only can be the outermost frame since we are
1220 just about to execute ENTRY. SP hasn't been set yet.
1221 We can assume any frame size, because it does not
1222 matter, and, let's fake frame base in cache. */
1223 cache->base = cache->prev_sp + 16;
1226 cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1227 cache->ps = (ps & ~PS_CALLINC_MASK)
1228 | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1234 fp_regnum = xtensa_scan_prologue (gdbarch, pc);
1235 ra = get_frame_register_unsigned (this_frame,
1236 gdbarch_tdep (gdbarch)->a0_base);
1237 cache->wd.callsize = WINSIZE (ra);
1238 cache->wd.wb = (wb - cache->wd.callsize / 4)
1239 & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
1240 cache->wd.ws = ws & ~(1 << wb);
1242 cache->pc = get_frame_func (this_frame);
1243 cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1244 cache->ps = (ps & ~PS_CALLINC_MASK)
1245 | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1248 if (cache->wd.ws == 0)
1253 sp = get_frame_register_unsigned
1254 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
1256 for (i = 0; i < 4; i++, sp += 4)
1258 cache->wd.aregs[i] = sp;
1261 if (cache->wd.callsize > 4)
1263 /* Set A4...A7/A11. */
1264 /* Get the SP of the frame previous to the previous one.
1265 To achieve this, we have to dereference SP twice. */
1266 sp = (CORE_ADDR) read_memory_integer (sp - 12, 4);
1267 sp = (CORE_ADDR) read_memory_integer (sp - 12, 4);
1268 sp -= cache->wd.callsize * 4;
1270 for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
1272 cache->wd.aregs[i] = sp;
1277 if ((cache->prev_sp == 0) && ( ra != 0 ))
1278 /* If RA is equal to 0 this frame is an outermost frame. Leave
1279 cache->prev_sp unchanged marking the boundary of the frame stack. */
1281 if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
1283 /* Register window overflow already happened.
1284 We can read caller's SP from the proper spill loction. */
1285 sp = get_frame_register_unsigned
1286 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1287 cache->prev_sp = read_memory_integer (sp - 12, 4);
1291 /* Read caller's frame SP directly from the previous window. */
1292 int regnum = arreg_number
1293 (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
1296 cache->prev_sp = get_frame_register_unsigned (this_frame, regnum);
1300 else /* Call0 framework. */
1302 unsigned int litbase_regnum = gdbarch_tdep (gdbarch)->litbase_regnum;
1303 CORE_ADDR litbase = (litbase_regnum == -1)
1304 ? 0 : get_frame_register_unsigned (this_frame, litbase_regnum);
1306 call0_frame_cache (this_frame, cache, pc, litbase);
1307 fp_regnum = cache->c0.fp_regnum;
1310 cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
1316 xtensa_frame_this_id (struct frame_info *this_frame,
1318 struct frame_id *this_id)
1320 struct xtensa_frame_cache *cache =
1321 xtensa_frame_cache (this_frame, this_cache);
1323 if (cache->prev_sp == 0)
1326 (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
1329 static struct value *
1330 xtensa_frame_prev_register (struct frame_info *this_frame,
1334 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1335 struct xtensa_frame_cache *cache;
1336 ULONGEST saved_reg = 0;
1339 if (*this_cache == NULL)
1340 *this_cache = xtensa_frame_cache (this_frame, this_cache);
1341 cache = *this_cache;
1343 if (regnum ==gdbarch_pc_regnum (gdbarch))
1344 saved_reg = cache->ra;
1345 else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
1346 saved_reg = cache->prev_sp;
1347 else if (!cache->call0)
1349 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
1350 saved_reg = cache->wd.ws;
1351 else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
1352 saved_reg = cache->wd.wb;
1353 else if (regnum == gdbarch_ps_regnum (gdbarch))
1354 saved_reg = cache->ps;
1362 return frame_unwind_got_constant (this_frame, regnum, saved_reg);
1364 if (!cache->call0) /* Windowed ABI. */
1366 /* Convert A-register numbers to AR-register numbers,
1367 if we deal with A-register. */
1368 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
1369 && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
1370 regnum = arreg_number (gdbarch, regnum, cache->wd.wb);
1372 /* Check, if we deal with AR-register saved on stack. */
1373 if (regnum >= gdbarch_tdep (gdbarch)->ar_base
1374 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1375 + gdbarch_tdep (gdbarch)->num_aregs))
1377 int areg = areg_number (gdbarch, regnum, cache->wd.wb);
1380 && areg < XTENSA_NUM_SAVED_AREGS
1381 && cache->wd.aregs[areg] != -1)
1382 return frame_unwind_got_memory (this_frame, regnum,
1383 cache->wd.aregs[areg]);
1386 else /* Call0 ABI. */
1388 int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
1389 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1391 ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
1398 /* If register was saved in the prologue, retrieve it. */
1399 stkofs = cache->c0.c0_rt[reg].to_stk;
1400 if (stkofs != C0_NOSTK)
1402 /* Determine SP on entry based on FP. */
1403 spe = cache->c0.c0_fp
1404 - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1406 return frame_unwind_got_memory (this_frame, regnum, spe + stkofs);
1411 /* All other registers have been either saved to
1412 the stack or are still alive in the processor. */
1414 return frame_unwind_got_register (this_frame, regnum, regnum);
1418 static const struct frame_unwind
1422 xtensa_frame_this_id,
1423 xtensa_frame_prev_register,
1425 default_frame_sniffer
1429 xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
1431 struct xtensa_frame_cache *cache =
1432 xtensa_frame_cache (this_frame, this_cache);
1437 static const struct frame_base
1441 xtensa_frame_base_address,
1442 xtensa_frame_base_address,
1443 xtensa_frame_base_address
1448 xtensa_extract_return_value (struct type *type,
1449 struct regcache *regcache,
1452 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1453 bfd_byte *valbuf = dst;
1454 int len = TYPE_LENGTH (type);
1459 DEBUGTRACE ("xtensa_extract_return_value (...)\n");
1461 gdb_assert(len > 0);
1463 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1465 /* First, we have to find the caller window in the register file. */
1466 regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1467 callsize = extract_call_winsize (gdbarch, pc);
1469 /* On Xtensa, we can return up to 4 words (or 2 for call12). */
1470 if (len > (callsize > 8 ? 8 : 16))
1471 internal_error (__FILE__, __LINE__,
1472 _("cannot extract return value of %d bytes long"), len);
1474 /* Get the register offset of the return
1475 register (A2) in the caller window. */
1476 regcache_raw_read_unsigned
1477 (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1478 areg = arreg_number (gdbarch,
1479 gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1483 /* No windowing hardware - Call0 ABI. */
1484 areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1487 DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
1489 if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1492 for (; len > 0; len -= 4, areg++, valbuf += 4)
1495 regcache_raw_read_part (regcache, areg, offset, len, valbuf);
1497 regcache_raw_read (regcache, areg, valbuf);
1503 xtensa_store_return_value (struct type *type,
1504 struct regcache *regcache,
1507 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1508 const bfd_byte *valbuf = dst;
1512 int len = TYPE_LENGTH (type);
1515 DEBUGTRACE ("xtensa_store_return_value (...)\n");
1517 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1519 regcache_raw_read_unsigned
1520 (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1521 regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1522 callsize = extract_call_winsize (gdbarch, pc);
1524 if (len > (callsize > 8 ? 8 : 16))
1525 internal_error (__FILE__, __LINE__,
1526 _("unimplemented for this length: %d"),
1527 TYPE_LENGTH (type));
1528 areg = arreg_number (gdbarch,
1529 gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1531 DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
1532 callsize, (int) wb);
1536 areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1539 if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1542 for (; len > 0; len -= 4, areg++, valbuf += 4)
1545 regcache_raw_write_part (regcache, areg, offset, len, valbuf);
1547 regcache_raw_write (regcache, areg, valbuf);
1552 static enum return_value_convention
1553 xtensa_return_value (struct gdbarch *gdbarch,
1554 struct type *func_type,
1555 struct type *valtype,
1556 struct regcache *regcache,
1558 const gdb_byte *writebuf)
1560 /* Structures up to 16 bytes are returned in registers. */
1562 int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1563 || TYPE_CODE (valtype) == TYPE_CODE_UNION
1564 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1565 && TYPE_LENGTH (valtype) > 16);
1568 return RETURN_VALUE_STRUCT_CONVENTION;
1570 DEBUGTRACE ("xtensa_return_value(...)\n");
1572 if (writebuf != NULL)
1574 xtensa_store_return_value (valtype, regcache, writebuf);
1577 if (readbuf != NULL)
1579 gdb_assert (!struct_return);
1580 xtensa_extract_return_value (valtype, regcache, readbuf);
1582 return RETURN_VALUE_REGISTER_CONVENTION;
1589 xtensa_push_dummy_call (struct gdbarch *gdbarch,
1590 struct value *function,
1591 struct regcache *regcache,
1594 struct value **args,
1597 CORE_ADDR struct_addr)
1600 int size, onstack_size;
1601 gdb_byte *buf = (gdb_byte *) alloca (16);
1603 struct argument_info
1605 const bfd_byte *contents;
1607 int onstack; /* onstack == 0 => in reg */
1608 int align; /* alignment */
1611 int offset; /* stack offset if on stack */
1612 int regno; /* regno if in register */
1616 struct argument_info *arg_info =
1617 (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
1621 DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
1623 if (xtensa_debug_level > 3)
1626 DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
1627 DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
1628 "struct_addr=0x%x\n",
1629 (int) sp, (int) struct_return, (int) struct_addr);
1631 for (i = 0; i < nargs; i++)
1633 struct value *arg = args[i];
1634 struct type *arg_type = check_typedef (value_type (arg));
1635 fprintf_unfiltered (gdb_stdlog, "%2d: 0x%lx %3d ",
1636 i, (unsigned long) arg, TYPE_LENGTH (arg_type));
1637 switch (TYPE_CODE (arg_type))
1640 fprintf_unfiltered (gdb_stdlog, "int");
1642 case TYPE_CODE_STRUCT:
1643 fprintf_unfiltered (gdb_stdlog, "struct");
1646 fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
1649 fprintf_unfiltered (gdb_stdlog, " 0x%lx\n",
1650 (unsigned long) value_contents (arg));
1654 /* First loop: collect information.
1655 Cast into type_long. (This shouldn't happen often for C because
1656 GDB already does this earlier.) It's possible that GDB could
1657 do it all the time but it's harmless to leave this code here. */
1664 size = REGISTER_SIZE;
1666 for (i = 0; i < nargs; i++)
1668 struct argument_info *info = &arg_info[i];
1669 struct value *arg = args[i];
1670 struct type *arg_type = check_typedef (value_type (arg));
1672 switch (TYPE_CODE (arg_type))
1675 case TYPE_CODE_BOOL:
1676 case TYPE_CODE_CHAR:
1677 case TYPE_CODE_RANGE:
1678 case TYPE_CODE_ENUM:
1680 /* Cast argument to long if necessary as the mask does it too. */
1681 if (TYPE_LENGTH (arg_type)
1682 < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
1684 arg_type = builtin_type (gdbarch)->builtin_long;
1685 arg = value_cast (arg_type, arg);
1687 /* Aligment is equal to the type length for the basic types. */
1688 info->align = TYPE_LENGTH (arg_type);
1693 /* Align doubles correctly. */
1694 if (TYPE_LENGTH (arg_type)
1695 == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
1696 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
1698 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1701 case TYPE_CODE_STRUCT:
1703 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1706 info->length = TYPE_LENGTH (arg_type);
1707 info->contents = value_contents (arg);
1709 /* Align size and onstack_size. */
1710 size = (size + info->align - 1) & ~(info->align - 1);
1711 onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
1713 if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
1716 info->u.offset = onstack_size;
1717 onstack_size += info->length;
1722 info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
1724 size += info->length;
1727 /* Adjust the stack pointer and align it. */
1728 sp = align_down (sp - onstack_size, SP_ALIGNMENT);
1730 /* Simulate MOVSP, if Windowed ABI. */
1731 if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1734 read_memory (osp - 16, buf, 16);
1735 write_memory (sp - 16, buf, 16);
1738 /* Second Loop: Load arguments. */
1742 store_unsigned_integer (buf, REGISTER_SIZE, struct_addr);
1743 regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
1746 for (i = 0; i < nargs; i++)
1748 struct argument_info *info = &arg_info[i];
1752 int n = info->length;
1753 CORE_ADDR offset = sp + info->u.offset;
1755 /* Odd-sized structs are aligned to the lower side of a memory
1756 word in big-endian mode and require a shift. This only
1757 applies for structures smaller than one word. */
1759 if (n < REGISTER_SIZE
1760 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1761 offset += (REGISTER_SIZE - n);
1763 write_memory (offset, info->contents, info->length);
1768 int n = info->length;
1769 const bfd_byte *cp = info->contents;
1770 int r = info->u.regno;
1772 /* Odd-sized structs are aligned to the lower side of registers in
1773 big-endian mode and require a shift. The odd-sized leftover will
1774 be at the end. Note that this is only true for structures smaller
1775 than REGISTER_SIZE; for larger odd-sized structures the excess
1776 will be left-aligned in the register on both endiannesses. */
1778 if (n < REGISTER_SIZE
1779 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1781 ULONGEST v = extract_unsigned_integer (cp, REGISTER_SIZE);
1782 v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
1784 store_unsigned_integer (buf, REGISTER_SIZE, v);
1785 regcache_cooked_write (regcache, r, buf);
1787 cp += REGISTER_SIZE;
1794 regcache_cooked_write (regcache, r, cp);
1796 cp += REGISTER_SIZE;
1803 /* Set the return address of dummy frame to the dummy address.
1804 The return address for the current function (in A0) is
1805 saved in the dummy frame, so we can savely overwrite A0 here. */
1807 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1809 ra = (bp_addr & 0x3fffffff) | 0x40000000;
1810 regcache_raw_read (regcache, gdbarch_ps_regnum (gdbarch), buf);
1811 ps = extract_unsigned_integer (buf, 4) & ~0x00030000;
1812 regcache_cooked_write_unsigned
1813 (regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
1814 regcache_cooked_write_unsigned (regcache,
1815 gdbarch_ps_regnum (gdbarch),
1818 /* All the registers have been saved. After executing
1819 dummy call, they all will be restored. So it's safe
1820 to modify WINDOWSTART register to make it look like there
1821 is only one register window corresponding to WINDOWEBASE. */
1823 regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
1824 regcache_cooked_write_unsigned (regcache,
1825 gdbarch_tdep (gdbarch)->ws_regnum,
1826 1 << extract_unsigned_integer (buf, 4));
1830 /* Simulate CALL0: write RA into A0 register. */
1831 regcache_cooked_write_unsigned
1832 (regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
1835 /* Set new stack pointer and return it. */
1836 regcache_cooked_write_unsigned (regcache,
1837 gdbarch_tdep (gdbarch)->a0_base + 1, sp);
1838 /* Make dummy frame ID unique by adding a constant. */
1839 return sp + SP_ALIGNMENT;
1843 /* Return a breakpoint for the current location of PC. We always use
1844 the density version if we have density instructions (regardless of the
1845 current instruction at PC), and use regular instructions otherwise. */
1847 #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
1848 #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
1849 #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
1850 #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
1852 static const unsigned char *
1853 xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1856 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
1857 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
1858 static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
1859 static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;
1861 DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
1863 if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
1865 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1867 *lenptr = sizeof (density_big_breakpoint);
1868 return density_big_breakpoint;
1872 *lenptr = sizeof (density_little_breakpoint);
1873 return density_little_breakpoint;
1878 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1880 *lenptr = sizeof (big_breakpoint);
1881 return big_breakpoint;
1885 *lenptr = sizeof (little_breakpoint);
1886 return little_breakpoint;
1891 /* Call0 ABI support routines. */
1893 /* Call0 opcode class. Opcodes are preclassified according to what they
1894 mean for Call0 prologue analysis, and their number of significant operands.
1895 The purpose of this is to simplify prologue analysis by separating
1896 instruction decoding (libisa) from the semantics of prologue analysis. */
1899 c0opc_illegal, /* Unknown to libisa (invalid) or 'ill' opcode. */
1900 c0opc_uninteresting, /* Not interesting for Call0 prologue analysis. */
1901 c0opc_flow, /* Flow control insn. */
1902 c0opc_entry, /* ENTRY indicates non-Call0 prologue. */
1903 c0opc_break, /* Debugger software breakpoints. */
1904 c0opc_add, /* Adding two registers. */
1905 c0opc_addi, /* Adding a register and an immediate. */
1906 c0opc_sub, /* Subtracting a register from a register. */
1907 c0opc_mov, /* Moving a register to a register. */
1908 c0opc_movi, /* Moving an immediate to a register. */
1909 c0opc_l32r, /* Loading a literal. */
1910 c0opc_s32i, /* Storing word at fixed offset from a base register. */
1911 c0opc_NrOf /* Number of opcode classifications. */
1915 /* Classify an opcode based on what it means for Call0 prologue analysis. */
1917 static xtensa_insn_kind
1918 call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
1920 const char *opcname;
1921 xtensa_insn_kind opclass = c0opc_uninteresting;
1923 DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
1925 /* Get opcode name and handle special classifications. */
1927 opcname = xtensa_opcode_name (isa, opc);
1930 || strcasecmp (opcname, "ill") == 0
1931 || strcasecmp (opcname, "ill.n") == 0)
1932 opclass = c0opc_illegal;
1933 else if (strcasecmp (opcname, "break") == 0
1934 || strcasecmp (opcname, "break.n") == 0)
1935 opclass = c0opc_break;
1936 else if (strcasecmp (opcname, "entry") == 0)
1937 opclass = c0opc_entry;
1938 else if (xtensa_opcode_is_branch (isa, opc) > 0
1939 || xtensa_opcode_is_jump (isa, opc) > 0
1940 || xtensa_opcode_is_loop (isa, opc) > 0
1941 || xtensa_opcode_is_call (isa, opc) > 0
1942 || strcasecmp (opcname, "simcall") == 0
1943 || strcasecmp (opcname, "syscall") == 0)
1944 opclass = c0opc_flow;
1946 /* Also, classify specific opcodes that need to be tracked. */
1947 else if (strcasecmp (opcname, "add") == 0
1948 || strcasecmp (opcname, "add.n") == 0)
1949 opclass = c0opc_add;
1950 else if (strcasecmp (opcname, "addi") == 0
1951 || strcasecmp (opcname, "addi.n") == 0
1952 || strcasecmp (opcname, "addmi") == 0)
1953 opclass = c0opc_addi;
1954 else if (strcasecmp (opcname, "sub") == 0)
1955 opclass = c0opc_sub;
1956 else if (strcasecmp (opcname, "mov.n") == 0
1957 || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro. */
1958 opclass = c0opc_mov;
1959 else if (strcasecmp (opcname, "movi") == 0
1960 || strcasecmp (opcname, "movi.n") == 0)
1961 opclass = c0opc_movi;
1962 else if (strcasecmp (opcname, "l32r") == 0)
1963 opclass = c0opc_l32r;
1964 else if (strcasecmp (opcname, "s32i") == 0
1965 || strcasecmp (opcname, "s32i.n") == 0)
1966 opclass = c0opc_s32i;
1971 /* Tracks register movement/mutation for a given operation, which may
1972 be within a bundle. Updates the destination register tracking info
1973 accordingly. The pc is needed only for pc-relative load instructions
1974 (eg. l32r). The SP register number is needed to identify stores to
1978 call0_track_op (xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
1979 xtensa_insn_kind opclass, int nods, unsigned odv[],
1980 CORE_ADDR pc, CORE_ADDR litbase, int spreg)
1982 unsigned litaddr, litval;
1987 /* 3 operands: dst, src, imm. */
1988 gdb_assert (nods == 3);
1989 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1990 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
1993 /* 3 operands: dst, src1, src2. */
1994 gdb_assert (nods == 3);
1995 if (src[odv[1]].fr_reg == C0_CONST)
1997 dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
1998 dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
2000 else if (src[odv[2]].fr_reg == C0_CONST)
2002 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2003 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
2005 else dst[odv[0]].fr_reg = C0_INEXP;
2008 /* 3 operands: dst, src1, src2. */
2009 gdb_assert (nods == 3);
2010 if (src[odv[2]].fr_reg == C0_CONST)
2012 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2013 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
2015 else dst[odv[0]].fr_reg = C0_INEXP;
2018 /* 2 operands: dst, src [, src]. */
2019 gdb_assert (nods == 2);
2020 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2021 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
2024 /* 2 operands: dst, imm. */
2025 gdb_assert (nods == 2);
2026 dst[odv[0]].fr_reg = C0_CONST;
2027 dst[odv[0]].fr_ofs = odv[1];
2030 /* 2 operands: dst, literal offset. */
2031 gdb_assert (nods == 2);
2032 litaddr = litbase & 1
2033 ? (litbase & ~1) + (signed)odv[1]
2034 : (pc + 3 + (signed)odv[1]) & ~3;
2035 litval = read_memory_integer(litaddr, 4);
2036 dst[odv[0]].fr_reg = C0_CONST;
2037 dst[odv[0]].fr_ofs = litval;
2040 /* 3 operands: value, base, offset. */
2041 gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
2042 if (src[odv[1]].fr_reg == spreg /* Store to stack frame. */
2043 && (src[odv[1]].fr_ofs & 3) == 0 /* Alignment preserved. */
2044 && src[odv[0]].fr_reg >= 0 /* Value is from a register. */
2045 && src[odv[0]].fr_ofs == 0 /* Value hasn't been modified. */
2046 && src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time. */
2048 /* ISA encoding guarantees alignment. But, check it anyway. */
2049 gdb_assert ((odv[2] & 3) == 0);
2050 dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
2058 /* Analyze prologue of the function at start address to determine if it uses
2059 the Call0 ABI, and if so track register moves and linear modifications
2060 in the prologue up to the PC or just beyond the prologue, whichever is first.
2061 An 'entry' instruction indicates non-Call0 ABI and the end of the prologue.
2062 The prologue may overlap non-prologue instructions but is guaranteed to end
2063 by the first flow-control instruction (jump, branch, call or return).
2064 Since an optimized function may move information around and change the
2065 stack frame arbitrarily during the prologue, the information is guaranteed
2066 valid only at the point in the function indicated by the PC.
2067 May be used to skip the prologue or identify the ABI, w/o tracking.
2069 Returns: Address of first instruction after prologue, or PC (whichever
2070 is first), or 0, if decoding failed (in libisa).
2072 start Start address of function/prologue.
2073 pc Program counter to stop at. Use 0 to continue to end of prologue.
2074 If 0, avoids infinite run-on in corrupt code memory by bounding
2075 the scan to the end of the function if that can be determined.
2076 nregs Number of general registers to track (size of rt[] array).
2078 rt[] Array[nregs] of xtensa_c0reg structures for register tracking info.
2079 If NULL, registers are not tracked.
2081 call0 If != NULL, *call0 is set non-zero if Call0 ABI used, else 0
2082 (more accurately, non-zero until 'entry' insn is encountered).
2084 Note that these may produce useful results even if decoding fails
2085 because they begin with default assumptions that analysis may change. */
2088 call0_analyze_prologue (CORE_ADDR start, CORE_ADDR pc, CORE_ADDR litbase,
2089 int nregs, xtensa_c0reg_t rt[], int *call0)
2091 CORE_ADDR ia; /* Current insn address in prologue. */
2092 CORE_ADDR ba = 0; /* Current address at base of insn buffer. */
2093 CORE_ADDR bt; /* Current address at top+1 of insn buffer. */
2094 #define BSZ 32 /* Instruction buffer size. */
2095 char ibuf[BSZ]; /* Instruction buffer for decoding prologue. */
2096 xtensa_isa isa; /* libisa ISA handle. */
2097 xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot. */
2098 xtensa_format ifmt; /* libisa instruction format. */
2099 int ilen, islots, is; /* Instruction length, nbr slots, current slot. */
2100 xtensa_opcode opc; /* Opcode in current slot. */
2101 xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis. */
2102 int nods; /* Opcode number of operands. */
2103 unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa. */
2104 xtensa_c0reg_t *rtmp; /* Register tracking info snapshot. */
2105 int j; /* General loop counter. */
2106 int fail = 0; /* Set non-zero and exit, if decoding fails. */
2107 CORE_ADDR body_pc; /* The PC for the first non-prologue insn. */
2108 CORE_ADDR end_pc; /* The PC for the lust function insn. */
2110 struct symtab_and_line prologue_sal;
2112 DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
2113 (int)start, (int)pc);
2115 /* Try to limit the scan to the end of the function if a non-zero pc
2116 arg was not supplied to avoid probing beyond the end of valid memory.
2117 If memory is full of garbage that classifies as c0opc_uninteresting.
2118 If this fails (eg. if no symbols) pc ends up 0 as it was.
2119 Intialize the Call0 frame and register tracking info.
2120 Assume it's Call0 until an 'entry' instruction is encountered.
2121 Assume we may be in the prologue until we hit a flow control instr. */
2127 /* Find out, if we have an information about the prologue from DWARF. */
2128 prologue_sal = find_pc_line (start, 0);
2129 if (prologue_sal.line != 0) /* Found debug info. */
2130 body_pc = prologue_sal.end;
2132 /* If we are going to analyze the prologue in general without knowing about
2133 the current PC, make the best assumtion for the end of the prologue. */
2136 find_pc_partial_function (start, 0, NULL, &end_pc);
2137 body_pc = min (end_pc, body_pc);
2140 body_pc = min (pc, body_pc);
2147 rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
2148 /* rt is already initialized in xtensa_alloc_frame_cache(). */
2152 if (!xtensa_default_isa)
2153 xtensa_default_isa = xtensa_isa_init (0, 0);
2154 isa = xtensa_default_isa;
2155 gdb_assert (BSZ >= xtensa_isa_maxlength (isa));
2156 ins = xtensa_insnbuf_alloc (isa);
2157 slot = xtensa_insnbuf_alloc (isa);
2159 for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
2161 /* (Re)fill instruction buffer from memory if necessary, but do not
2162 read memory beyond PC to be sure we stay within text section
2163 (this protection only works if a non-zero pc is supplied). */
2165 if (ia + xtensa_isa_maxlength (isa) > bt)
2168 bt = (ba + BSZ) < body_pc ? ba + BSZ : body_pc;
2169 read_memory (ba, ibuf, bt - ba);
2172 /* Decode format information. */
2174 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2175 ifmt = xtensa_format_decode (isa, ins);
2176 if (ifmt == XTENSA_UNDEFINED)
2181 ilen = xtensa_format_length (isa, ifmt);
2182 if (ilen == XTENSA_UNDEFINED)
2187 islots = xtensa_format_num_slots (isa, ifmt);
2188 if (islots == XTENSA_UNDEFINED)
2194 /* Analyze a bundle or a single instruction, using a snapshot of
2195 the register tracking info as input for the entire bundle so that
2196 register changes do not take effect within this bundle. */
2198 for (j = 0; j < nregs; ++j)
2201 for (is = 0; is < islots; ++is)
2203 /* Decode a slot and classify the opcode. */
2205 fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
2209 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2210 DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
2212 if (opc == XTENSA_UNDEFINED)
2213 opclass = c0opc_illegal;
2215 opclass = call0_classify_opcode (isa, opc);
2217 /* Decide whether to track this opcode, ignore it, or bail out. */
2226 case c0opc_uninteresting:
2235 ia += ilen; /* Skip over 'entry' insn. */
2243 /* Only expected opcodes should get this far. */
2247 /* Extract and decode the operands. */
2248 nods = xtensa_opcode_num_operands (isa, opc);
2249 if (nods == XTENSA_UNDEFINED)
2255 for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
2257 fail = xtensa_operand_get_field (isa, opc, j, ifmt,
2262 fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
2267 /* Check operands to verify use of 'mov' assembler macro. */
2268 if (opclass == c0opc_mov && nods == 3)
2270 if (odv[2] == odv[1])
2274 opclass = c0opc_uninteresting;
2279 /* Track register movement and modification for this operation. */
2280 call0_track_op (rt, rtmp, opclass, nods, odv, ia, litbase, 1);
2284 DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
2285 (unsigned)ia, fail ? "failed" : "succeeded");
2286 xtensa_insnbuf_free(isa, slot);
2287 xtensa_insnbuf_free(isa, ins);
2288 return fail ? 0 : ia;
2291 /* Initialize frame cache for the current frame in CALL0 ABI. */
2294 call0_frame_cache (struct frame_info *this_frame,
2295 xtensa_frame_cache_t *cache, CORE_ADDR pc, CORE_ADDR litbase)
2297 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2298 CORE_ADDR start_pc; /* The beginning of the function. */
2299 CORE_ADDR body_pc=UINT_MAX; /* PC, where prologue analysis stopped. */
2300 CORE_ADDR sp, fp, ra;
2301 int fp_regnum, c0_hasfp, c0_frmsz, prev_sp, to_stk;
2303 /* Find the beginning of the prologue of the function containing the PC
2304 and analyze it up to the PC or the end of the prologue. */
2306 if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
2308 body_pc = call0_analyze_prologue (start_pc, pc, litbase, C0_NREGS,
2309 &cache->c0.c0_rt[0],
2313 sp = get_frame_register_unsigned
2314 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
2315 fp = sp; /* Assume FP == SP until proven otherwise. */
2317 /* Get the frame information and FP (if used) at the current PC.
2318 If PC is in the prologue, the prologue analysis is more reliable
2319 than DWARF info. We don't not know for sure if PC is in the prologue,
2320 but we know no calls have yet taken place, so we can almost
2321 certainly rely on the prologue analysis. */
2325 /* Prologue analysis was successful up to the PC.
2326 It includes the cases when PC == START_PC. */
2327 c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
2328 /* c0_hasfp == true means there is a frame pointer because
2329 we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
2330 was derived from SP. Otherwise, it would be C0_FP. */
2331 fp_regnum = c0_hasfp ? C0_FP : C0_SP;
2332 c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
2333 fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
2335 else /* No data from the prologue analysis. */
2338 fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
2343 prev_sp = fp + c0_frmsz;
2345 /* Frame size from debug info or prologue tracking does not account for
2346 alloca() and other dynamic allocations. Adjust frame size by FP - SP. */
2349 fp = get_frame_register_unsigned (this_frame, fp_regnum);
2351 /* Recalculate previous SP. */
2352 prev_sp = fp + c0_frmsz;
2353 /* Update the stack frame size. */
2354 c0_frmsz += fp - sp;
2357 /* Get the return address (RA) from the stack if saved,
2358 or try to get it from a register. */
2360 to_stk = cache->c0.c0_rt[C0_RA].to_stk;
2361 if (to_stk != C0_NOSTK)
2363 read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk, 4);
2365 else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
2366 && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
2368 /* Special case for terminating backtrace at a function that wants to
2369 be seen as the outermost. Such a function will clear it's RA (A0)
2370 register to 0 in the prologue instead of saving its original value. */
2375 /* RA was copied to another register or (before any function call) may
2376 still be in the original RA register. This is not always reliable:
2377 even in a leaf function, register tracking stops after prologue, and
2378 even in prologue, non-prologue instructions (not tracked) may overwrite
2379 RA or any register it was copied to. If likely in prologue or before
2380 any call, use retracking info and hope for the best (compiler should
2381 have saved RA in stack if not in a leaf function). If not in prologue,
2387 (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
2389 if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
2393 ra = get_frame_register_unsigned
2395 gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
2400 cache->pc = start_pc;
2402 /* RA == 0 marks the outermost frame. Do not go past it. */
2403 cache->prev_sp = (ra != 0) ? prev_sp : 0;
2404 cache->c0.fp_regnum = fp_regnum;
2405 cache->c0.c0_frmsz = c0_frmsz;
2406 cache->c0.c0_hasfp = c0_hasfp;
2407 cache->c0.c0_fp = fp;
2411 /* Skip function prologue.
2413 Return the pc of the first instruction after prologue. GDB calls this to
2414 find the address of the first line of the function or (if there is no line
2415 number information) to skip the prologue for planting breakpoints on
2416 function entries. Use debug info (if present) or prologue analysis to skip
2417 the prologue to achieve reliable debugging behavior. For windowed ABI,
2418 only the 'entry' instruction is skipped. It is not strictly necessary to
2419 skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
2420 backtrace at any point in the prologue, however certain potential hazards
2421 are avoided and a more "normal" debugging experience is ensured by
2422 skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
2423 For example, if we don't skip the prologue:
2424 - Some args may not yet have been saved to the stack where the debug
2425 info expects to find them (true anyway when only 'entry' is skipped);
2426 - Software breakpoints ('break' instrs) may not have been unplanted
2427 when the prologue analysis is done on initializing the frame cache,
2428 and breaks in the prologue will throw off the analysis.
2430 If we have debug info ( line-number info, in particular ) we simply skip
2431 the code associated with the first function line effectively skipping
2432 the prologue code. It works even in cases like
2435 { int local_var = 1;
2439 because, for this source code, both Xtensa compilers will generate two
2440 separate entries ( with the same line number ) in dwarf line-number
2441 section to make sure there is a boundary between the prologue code and
2442 the rest of the function.
2444 If there is no debug info, we need to analyze the code. */
2446 /* #define DONT_SKIP_PROLOGUE */
2449 xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
2451 struct symtab_and_line prologue_sal;
2454 DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
2456 #if DONT_SKIP_PROLOGUE
2460 /* Try to find first body line from debug info. */
2462 prologue_sal = find_pc_line (start_pc, 0);
2463 if (prologue_sal.line != 0) /* Found debug info. */
2465 /* In Call0, it is possible to have a function with only one instruction
2466 ('ret') resulting from a 1-line optimized function that does nothing.
2467 In that case, prologue_sal.end may actually point to the start of the
2468 next function in the text section, causing a breakpoint to be set at
2469 the wrong place. Check if the end address is in a different function,
2470 and if so return the start PC. We know we have symbol info. */
2474 find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
2475 if (end_func != start_pc)
2478 return prologue_sal.end;
2481 /* No debug line info. Analyze prologue for Call0 or simply skip ENTRY. */
2482 body_pc = call0_analyze_prologue(start_pc, 0, 0, 0, NULL, NULL);
2483 return body_pc != 0 ? body_pc : start_pc;
2486 /* Verify the current configuration. */
2488 xtensa_verify_config (struct gdbarch *gdbarch)
2490 struct ui_file *log;
2491 struct cleanup *cleanups;
2492 struct gdbarch_tdep *tdep;
2496 tdep = gdbarch_tdep (gdbarch);
2497 log = mem_fileopen ();
2498 cleanups = make_cleanup_ui_file_delete (log);
2500 /* Verify that we got a reasonable number of AREGS. */
2501 if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
2502 fprintf_unfiltered (log, _("\
2503 \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
2506 /* Verify that certain registers exist. */
2508 if (tdep->pc_regnum == -1)
2509 fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
2510 if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
2511 fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));
2513 if (tdep->isa_use_windowed_registers)
2515 if (tdep->wb_regnum == -1)
2516 fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
2517 if (tdep->ws_regnum == -1)
2518 fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
2519 if (tdep->ar_base == -1)
2520 fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
2523 if (tdep->a0_base == -1)
2524 fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
2526 buf = ui_file_xstrdup (log, &dummy);
2527 make_cleanup (xfree, buf);
2528 if (strlen (buf) > 0)
2529 internal_error (__FILE__, __LINE__,
2530 _("the following are invalid: %s"), buf);
2531 do_cleanups (cleanups);
2535 /* Derive specific register numbers from the array of registers. */
2538 xtensa_derive_tdep (struct gdbarch_tdep *tdep)
2540 xtensa_register_t* rmap;
2541 int n, max_size = 4;
2544 tdep->num_nopriv_regs = 0;
2546 /* Special registers 0..255 (core). */
2547 #define XTENSA_DBREGN_SREG(n) (0x0200+(n))
2549 for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
2551 if (rmap->target_number == 0x0020)
2552 tdep->pc_regnum = n;
2553 else if (rmap->target_number == 0x0100)
2555 else if (rmap->target_number == 0x0000)
2557 else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
2558 tdep->wb_regnum = n;
2559 else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
2560 tdep->ws_regnum = n;
2561 else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
2562 tdep->debugcause_regnum = n;
2563 else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
2564 tdep->exccause_regnum = n;
2565 else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
2566 tdep->excvaddr_regnum = n;
2567 else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
2568 tdep->lbeg_regnum = n;
2569 else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
2570 tdep->lend_regnum = n;
2571 else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
2572 tdep->lcount_regnum = n;
2573 else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
2574 tdep->sar_regnum = n;
2575 else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
2576 tdep->litbase_regnum = n;
2577 else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
2578 tdep->ps_regnum = n;
2580 else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
2581 tdep->interrupt_regnum = n;
2582 else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
2583 tdep->interrupt2_regnum = n;
2584 else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
2585 tdep->cpenable_regnum = n;
2588 if (rmap->byte_size > max_size)
2589 max_size = rmap->byte_size;
2590 if (rmap->mask != 0 && tdep->num_regs == 0)
2592 /* Find out out how to deal with priveleged registers.
2594 if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
2595 && tdep->num_nopriv_regs == 0)
2596 tdep->num_nopriv_regs = n;
2598 if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
2599 && tdep->num_regs == 0)
2603 /* Number of pseudo registers. */
2604 tdep->num_pseudo_regs = n - tdep->num_regs;
2606 /* Empirically determined maximum sizes. */
2607 tdep->max_register_raw_size = max_size;
2608 tdep->max_register_virtual_size = max_size;
2611 /* Module "constructor" function. */
2613 extern struct gdbarch_tdep xtensa_tdep;
2615 static struct gdbarch *
2616 xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2618 struct gdbarch_tdep *tdep;
2619 struct gdbarch *gdbarch;
2620 struct xtensa_abi_handler *abi_handler;
2622 DEBUGTRACE ("gdbarch_init()\n");
2624 /* We have to set the byte order before we call gdbarch_alloc. */
2625 info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
2627 tdep = &xtensa_tdep;
2628 gdbarch = gdbarch_alloc (&info, tdep);
2629 xtensa_derive_tdep (tdep);
2631 /* Verify our configuration. */
2632 xtensa_verify_config (gdbarch);
2634 /* Pseudo-Register read/write. */
2635 set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
2636 set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
2638 /* Set target information. */
2639 set_gdbarch_num_regs (gdbarch, tdep->num_regs);
2640 set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
2641 set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
2642 set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
2643 set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
2645 /* Renumber registers for known formats (stabs and dwarf2). */
2646 set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2647 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2649 /* We provide our own function to get register information. */
2650 set_gdbarch_register_name (gdbarch, xtensa_register_name);
2651 set_gdbarch_register_type (gdbarch, xtensa_register_type);
2653 /* To call functions from GDB using dummy frame */
2654 set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
2656 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2658 set_gdbarch_return_value (gdbarch, xtensa_return_value);
2660 /* Advance PC across any prologue instructions to reach "real" code. */
2661 set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
2663 /* Stack grows downward. */
2664 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2666 /* Set breakpoints. */
2667 set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
2669 /* After breakpoint instruction or illegal instruction, pc still
2670 points at break instruction, so don't decrement. */
2671 set_gdbarch_decr_pc_after_break (gdbarch, 0);
2673 /* We don't skip args. */
2674 set_gdbarch_frame_args_skip (gdbarch, 0);
2676 set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
2678 set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
2680 set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
2682 /* Frame handling. */
2683 frame_base_set_default (gdbarch, &xtensa_frame_base);
2684 frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
2685 dwarf2_append_unwinders (gdbarch);
2687 set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
2689 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
2691 xtensa_add_reggroups (gdbarch);
2692 set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
2694 set_gdbarch_regset_from_core_section (gdbarch,
2695 xtensa_regset_from_core_section);
2697 set_solib_svr4_fetch_link_map_offsets
2698 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
2704 xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
2706 error (_("xtensa_dump_tdep(): not implemented"));
2710 _initialize_xtensa_tdep (void)
2712 struct cmd_list_element *c;
2714 gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
2715 xtensa_init_reggroups ();
2717 add_setshow_zinteger_cmd ("xtensa",
2719 &xtensa_debug_level, _("\
2720 Set Xtensa debugging."), _("\
2721 Show Xtensa debugging."), _("\
2722 When non-zero, Xtensa-specific debugging is enabled. \
2723 Can be 1, 2, 3, or 4 indicating the level of debugging."),
2726 &setdebuglist, &showdebuglist);