1 /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
3 Copyright (C) 2003-2014 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"
38 #include "dwarf2-frame.h"
39 #include "dwarf2loc.h"
40 #include "frame-base.h"
41 #include "frame-unwind.h"
43 #include "arch-utils.h"
51 #include "xtensa-isa.h"
52 #include "xtensa-tdep.h"
53 #include "xtensa-config.h"
56 static unsigned int xtensa_debug_level = 0;
58 #define DEBUGWARN(args...) \
59 if (xtensa_debug_level > 0) \
60 fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
62 #define DEBUGINFO(args...) \
63 if (xtensa_debug_level > 1) \
64 fprintf_unfiltered (gdb_stdlog, "(info ) " args)
66 #define DEBUGTRACE(args...) \
67 if (xtensa_debug_level > 2) \
68 fprintf_unfiltered (gdb_stdlog, "(trace) " args)
70 #define DEBUGVERB(args...) \
71 if (xtensa_debug_level > 3) \
72 fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
75 /* According to the ABI, the SP must be aligned to 16-byte boundaries. */
76 #define SP_ALIGNMENT 16
79 /* On Windowed ABI, we use a6 through a11 for passing arguments
80 to a function called by GDB because CALL4 is used. */
81 #define ARGS_NUM_REGS 6
82 #define REGISTER_SIZE 4
85 /* Extract the call size from the return address or PS register. */
86 #define PS_CALLINC_SHIFT 16
87 #define PS_CALLINC_MASK 0x00030000
88 #define CALLINC(ps) (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
89 #define WINSIZE(ra) (4 * (( (ra) >> 30) & 0x3))
91 /* On TX, hardware can be configured without Exception Option.
92 There is no PS register in this case. Inside XT-GDB, let us treat
93 it as a virtual read-only register always holding the same value. */
96 /* ABI-independent macros. */
97 #define ARG_NOF(gdbarch) \
98 (gdbarch_tdep (gdbarch)->call_abi \
99 == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
100 #define ARG_1ST(gdbarch) \
101 (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only \
102 ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
103 : (gdbarch_tdep (gdbarch)->a0_base + 6))
105 /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
106 indicates that the instruction is an ENTRY instruction. */
108 #define XTENSA_IS_ENTRY(gdbarch, op1) \
109 ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
110 ? ((op1) == 0x6c) : ((op1) == 0x36))
112 #define XTENSA_ENTRY_LENGTH 3
114 /* windowing_enabled() returns true, if windowing is enabled.
115 WOE must be set to 1; EXCM to 0.
116 Note: We assume that EXCM is always 0 for XEA1. */
118 #define PS_WOE (1<<18)
119 #define PS_EXC (1<<4)
122 windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
124 /* If we know CALL0 ABI is set explicitly, say it is Call0. */
125 if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
128 return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
131 /* Convert a live A-register number to the corresponding AR-register
134 arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
136 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
139 arreg = a_regnum - tdep->a0_base;
140 arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
141 arreg &= tdep->num_aregs - 1;
143 return arreg + tdep->ar_base;
146 /* Convert a live AR-register number to the corresponding A-register order
147 number in a range [0..15]. Return -1, if AR_REGNUM is out of WB window. */
149 areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
151 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
154 areg = ar_regnum - tdep->ar_base;
155 if (areg < 0 || areg >= tdep->num_aregs)
157 areg = (areg - wb * 4) & (tdep->num_aregs - 1);
158 return (areg > 15) ? -1 : areg;
161 /* Read Xtensa register directly from the hardware. */
163 xtensa_read_register (int regnum)
167 regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
168 return (unsigned long) value;
171 /* Write Xtensa register directly to the hardware. */
173 xtensa_write_register (int regnum, ULONGEST value)
175 regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
178 /* Return the window size of the previous call to the function from which we
181 This function is used to extract the return value after a called function
182 has returned to the caller. On Xtensa, the register that holds the return
183 value (from the perspective of the caller) depends on what call
184 instruction was used. For now, we are assuming that the call instruction
185 precedes the current address, so we simply analyze the call instruction.
186 If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
187 method to call the inferior function. */
190 extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
192 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
197 DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
199 /* Read the previous instruction (should be a call[x]{4|8|12}. */
200 read_memory (pc-3, buf, 3);
201 insn = extract_unsigned_integer (buf, 3, byte_order);
203 /* Decode call instruction:
205 call{0,4,8,12} OFFSET || {00,01,10,11} || 0101
206 callx{0,4,8,12} OFFSET || 11 || {00,01,10,11} || 0000
208 call{0,4,8,12} 0101 || {00,01,10,11} || OFFSET
209 callx{0,4,8,12} 0000 || {00,01,10,11} || 11 || OFFSET. */
211 if (byte_order == BFD_ENDIAN_LITTLE)
213 if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
214 winsize = (insn & 0x30) >> 2; /* 0, 4, 8, 12. */
218 if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
219 winsize = (insn >> 16) & 0xc; /* 0, 4, 8, 12. */
225 /* REGISTER INFORMATION */
227 /* Find register by name. */
229 xtensa_find_register_by_name (struct gdbarch *gdbarch, char *name)
233 for (i = 0; i < gdbarch_num_regs (gdbarch)
234 + gdbarch_num_pseudo_regs (gdbarch);
237 if (strcasecmp (gdbarch_tdep (gdbarch)->regmap[i].name, name) == 0)
243 /* Returns the name of a register. */
245 xtensa_register_name (struct gdbarch *gdbarch, int regnum)
247 /* Return the name stored in the register map. */
248 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
249 + gdbarch_num_pseudo_regs (gdbarch))
250 return gdbarch_tdep (gdbarch)->regmap[regnum].name;
252 internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
256 /* Return the type of a register. Create a new type, if necessary. */
259 xtensa_register_type (struct gdbarch *gdbarch, int regnum)
261 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
263 /* Return signed integer for ARx and Ax registers. */
264 if ((regnum >= tdep->ar_base
265 && regnum < tdep->ar_base + tdep->num_aregs)
266 || (regnum >= tdep->a0_base
267 && regnum < tdep->a0_base + 16))
268 return builtin_type (gdbarch)->builtin_int;
270 if (regnum == gdbarch_pc_regnum (gdbarch)
271 || regnum == tdep->a0_base + 1)
272 return builtin_type (gdbarch)->builtin_data_ptr;
274 /* Return the stored type for all other registers. */
275 else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
276 + gdbarch_num_pseudo_regs (gdbarch))
278 xtensa_register_t* reg = &tdep->regmap[regnum];
280 /* Set ctype for this register (only the first time). */
284 struct ctype_cache *tp;
285 int size = reg->byte_size;
287 /* We always use the memory representation,
288 even if the register width is smaller. */
292 reg->ctype = builtin_type (gdbarch)->builtin_uint8;
296 reg->ctype = builtin_type (gdbarch)->builtin_uint16;
300 reg->ctype = builtin_type (gdbarch)->builtin_uint32;
304 reg->ctype = builtin_type (gdbarch)->builtin_uint64;
308 reg->ctype = builtin_type (gdbarch)->builtin_uint128;
312 for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
313 if (tp->size == size)
318 char *name = xstrprintf ("int%d", size * 8);
319 tp = xmalloc (sizeof (struct ctype_cache));
320 tp->next = tdep->type_entries;
321 tdep->type_entries = tp;
324 = arch_integer_type (gdbarch, size * 8, 1, name);
328 reg->ctype = tp->virtual_type;
334 internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
339 /* Return the 'local' register number for stubs, dwarf2, etc.
340 The debugging information enumerates registers starting from 0 for A0
341 to n for An. So, we only have to add the base number for A0. */
344 xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
348 if (regnum >= 0 && regnum < 16)
349 return gdbarch_tdep (gdbarch)->a0_base + regnum;
352 i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
354 if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
357 internal_error (__FILE__, __LINE__,
358 _("invalid dwarf/stabs register number %d"), regnum);
363 /* Write the bits of a masked register to the various registers.
364 Only the masked areas of these registers are modified; the other
365 fields are untouched. The size of masked registers is always less
366 than or equal to 32 bits. */
369 xtensa_register_write_masked (struct regcache *regcache,
370 xtensa_register_t *reg, const gdb_byte *buffer)
372 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
373 const xtensa_mask_t *mask = reg->mask;
375 int shift = 0; /* Shift for next mask (mod 32). */
376 int start, size; /* Start bit and size of current mask. */
378 unsigned int *ptr = value;
379 unsigned int regval, m, mem = 0;
381 int bytesize = reg->byte_size;
382 int bitsize = bytesize * 8;
385 DEBUGTRACE ("xtensa_register_write_masked ()\n");
387 /* Copy the masked register to host byte-order. */
388 if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
389 for (i = 0; i < bytesize; i++)
392 mem |= (buffer[bytesize - i - 1] << 24);
397 for (i = 0; i < bytesize; i++)
400 mem |= (buffer[i] << 24);
405 /* We might have to shift the final value:
406 bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
407 bytesize & 3 == x -> shift (4-x) * 8. */
409 *ptr = mem >> (((0 - bytesize) & 3) * 8);
413 /* Write the bits to the masked areas of the other registers. */
414 for (i = 0; i < mask->count; i++)
416 start = mask->mask[i].bit_start;
417 size = mask->mask[i].bit_size;
418 regval = mem >> shift;
420 if ((shift += size) > bitsize)
421 error (_("size of all masks is larger than the register"));
430 regval |= mem << (size - shift);
433 /* Make sure we have a valid register. */
434 r = mask->mask[i].reg_num;
435 if (r >= 0 && size > 0)
437 /* Don't overwrite the unmasked areas. */
439 regcache_cooked_read_unsigned (regcache, r, &old_val);
440 m = 0xffffffff >> (32 - size) << start;
442 regval = (regval & m) | (old_val & ~m);
443 regcache_cooked_write_unsigned (regcache, r, regval);
449 /* Read a tie state or mapped registers. Read the masked areas
450 of the registers and assemble them into a single value. */
452 static enum register_status
453 xtensa_register_read_masked (struct regcache *regcache,
454 xtensa_register_t *reg, gdb_byte *buffer)
456 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
457 const xtensa_mask_t *mask = reg->mask;
462 unsigned int *ptr = value;
463 unsigned int regval, mem = 0;
465 int bytesize = reg->byte_size;
466 int bitsize = bytesize * 8;
469 DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
470 reg->name == 0 ? "" : reg->name);
472 /* Assemble the register from the masked areas of other registers. */
473 for (i = 0; i < mask->count; i++)
475 int r = mask->mask[i].reg_num;
478 enum register_status status;
481 status = regcache_cooked_read_unsigned (regcache, r, &val);
482 if (status != REG_VALID)
484 regval = (unsigned int) val;
489 start = mask->mask[i].bit_start;
490 size = mask->mask[i].bit_size;
495 regval &= (0xffffffff >> (32 - size));
497 mem |= regval << shift;
499 if ((shift += size) > bitsize)
500 error (_("size of all masks is larger than the register"));
511 mem = regval >> (size - shift);
518 /* Copy value to target byte order. */
522 if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
523 for (i = 0; i < bytesize; i++)
527 buffer[bytesize - i - 1] = mem & 0xff;
531 for (i = 0; i < bytesize; i++)
535 buffer[i] = mem & 0xff;
543 /* Read pseudo registers. */
545 static enum register_status
546 xtensa_pseudo_register_read (struct gdbarch *gdbarch,
547 struct regcache *regcache,
551 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
553 DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
554 regnum, xtensa_register_name (gdbarch, regnum));
556 if (regnum == gdbarch_num_regs (gdbarch)
557 + gdbarch_num_pseudo_regs (gdbarch) - 1)
558 regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
560 /* Read aliases a0..a15, if this is a Windowed ABI. */
561 if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
562 && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
563 && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
565 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
566 enum register_status status;
568 status = regcache_raw_read (regcache,
569 gdbarch_tdep (gdbarch)->wb_regnum,
571 if (status != REG_VALID)
573 regnum = arreg_number (gdbarch, regnum,
574 extract_unsigned_integer (buf, 4, byte_order));
577 /* We can always read non-pseudo registers. */
578 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
579 return regcache_raw_read (regcache, regnum, buffer);
581 /* We have to find out how to deal with priveleged registers.
582 Let's treat them as pseudo-registers, but we cannot read/write them. */
584 else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
586 buffer[0] = (gdb_byte)0;
587 buffer[1] = (gdb_byte)0;
588 buffer[2] = (gdb_byte)0;
589 buffer[3] = (gdb_byte)0;
592 /* Pseudo registers. */
594 && regnum < gdbarch_num_regs (gdbarch)
595 + gdbarch_num_pseudo_regs (gdbarch))
597 xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
598 xtensa_register_type_t type = reg->type;
599 int flags = gdbarch_tdep (gdbarch)->target_flags;
601 /* We cannot read Unknown or Unmapped registers. */
602 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
604 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
606 warning (_("cannot read register %s"),
607 xtensa_register_name (gdbarch, regnum));
612 /* Some targets cannot read TIE register files. */
613 else if (type == xtRegisterTypeTieRegfile)
615 /* Use 'fetch' to get register? */
616 if (flags & xtTargetFlagsUseFetchStore)
618 warning (_("cannot read register"));
622 /* On some targets (esp. simulators), we can always read the reg. */
623 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
625 warning (_("cannot read register"));
630 /* We can always read mapped registers. */
631 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
632 return xtensa_register_read_masked (regcache, reg, buffer);
634 /* Assume that we can read the register. */
635 return regcache_raw_read (regcache, regnum, buffer);
638 internal_error (__FILE__, __LINE__,
639 _("invalid register number %d"), regnum);
643 /* Write pseudo registers. */
646 xtensa_pseudo_register_write (struct gdbarch *gdbarch,
647 struct regcache *regcache,
649 const gdb_byte *buffer)
651 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
653 DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
654 regnum, xtensa_register_name (gdbarch, regnum));
656 if (regnum == gdbarch_num_regs (gdbarch)
657 + gdbarch_num_pseudo_regs (gdbarch) -1)
658 regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
660 /* Renumber register, if aliase a0..a15 on Windowed ABI. */
661 if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
662 && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
663 && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
665 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
667 regcache_raw_read (regcache,
668 gdbarch_tdep (gdbarch)->wb_regnum, buf);
669 regnum = arreg_number (gdbarch, regnum,
670 extract_unsigned_integer (buf, 4, byte_order));
673 /* We can always write 'core' registers.
674 Note: We might have converted Ax->ARy. */
675 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
676 regcache_raw_write (regcache, regnum, buffer);
678 /* We have to find out how to deal with priveleged registers.
679 Let's treat them as pseudo-registers, but we cannot read/write them. */
681 else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
685 /* Pseudo registers. */
687 && regnum < gdbarch_num_regs (gdbarch)
688 + gdbarch_num_pseudo_regs (gdbarch))
690 xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
691 xtensa_register_type_t type = reg->type;
692 int flags = gdbarch_tdep (gdbarch)->target_flags;
694 /* On most targets, we cannot write registers
695 of type "Unknown" or "Unmapped". */
696 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
698 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
700 warning (_("cannot write register %s"),
701 xtensa_register_name (gdbarch, regnum));
706 /* Some targets cannot read TIE register files. */
707 else if (type == xtRegisterTypeTieRegfile)
709 /* Use 'store' to get register? */
710 if (flags & xtTargetFlagsUseFetchStore)
712 warning (_("cannot write register"));
716 /* On some targets (esp. simulators), we can always write
718 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
720 warning (_("cannot write register"));
725 /* We can always write mapped registers. */
726 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
728 xtensa_register_write_masked (regcache, reg, buffer);
732 /* Assume that we can write the register. */
733 regcache_raw_write (regcache, regnum, buffer);
736 internal_error (__FILE__, __LINE__,
737 _("invalid register number %d"), regnum);
740 static struct reggroup *xtensa_ar_reggroup;
741 static struct reggroup *xtensa_user_reggroup;
742 static struct reggroup *xtensa_vectra_reggroup;
743 static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
746 xtensa_init_reggroups (void)
749 char cpname[] = "cp0";
751 xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
752 xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
753 xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
755 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
758 xtensa_cp[i] = reggroup_new (cpname, USER_REGGROUP);
763 xtensa_add_reggroups (struct gdbarch *gdbarch)
767 /* Predefined groups. */
768 reggroup_add (gdbarch, all_reggroup);
769 reggroup_add (gdbarch, save_reggroup);
770 reggroup_add (gdbarch, restore_reggroup);
771 reggroup_add (gdbarch, system_reggroup);
772 reggroup_add (gdbarch, vector_reggroup);
773 reggroup_add (gdbarch, general_reggroup);
774 reggroup_add (gdbarch, float_reggroup);
776 /* Xtensa-specific groups. */
777 reggroup_add (gdbarch, xtensa_ar_reggroup);
778 reggroup_add (gdbarch, xtensa_user_reggroup);
779 reggroup_add (gdbarch, xtensa_vectra_reggroup);
781 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
782 reggroup_add (gdbarch, xtensa_cp[i]);
786 xtensa_coprocessor_register_group (struct reggroup *group)
790 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
791 if (group == xtensa_cp[i])
797 #define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
798 | XTENSA_REGISTER_FLAGS_WRITABLE \
799 | XTENSA_REGISTER_FLAGS_VOLATILE)
801 #define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
802 | XTENSA_REGISTER_FLAGS_WRITABLE)
805 xtensa_register_reggroup_p (struct gdbarch *gdbarch,
807 struct reggroup *group)
809 xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
810 xtensa_register_type_t type = reg->type;
811 xtensa_register_group_t rg = reg->group;
814 if (group == save_reggroup)
815 /* Every single register should be included into the list of registers
816 to be watched for changes while using -data-list-changed-registers. */
819 /* First, skip registers that are not visible to this target
820 (unknown and unmapped registers when not using ISS). */
822 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
824 if (group == all_reggroup)
826 if (group == xtensa_ar_reggroup)
827 return rg & xtRegisterGroupAddrReg;
828 if (group == xtensa_user_reggroup)
829 return rg & xtRegisterGroupUser;
830 if (group == float_reggroup)
831 return rg & xtRegisterGroupFloat;
832 if (group == general_reggroup)
833 return rg & xtRegisterGroupGeneral;
834 if (group == system_reggroup)
835 return rg & xtRegisterGroupState;
836 if (group == vector_reggroup || group == xtensa_vectra_reggroup)
837 return rg & xtRegisterGroupVectra;
838 if (group == restore_reggroup)
839 return (regnum < gdbarch_num_regs (gdbarch)
840 && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
841 cp_number = xtensa_coprocessor_register_group (group);
843 return rg & (xtRegisterGroupCP0 << cp_number);
849 /* Supply register REGNUM from the buffer specified by GREGS and LEN
850 in the general-purpose register set REGSET to register cache
851 REGCACHE. If REGNUM is -1 do this for all registers in REGSET. */
854 xtensa_supply_gregset (const struct regset *regset,
860 const xtensa_elf_gregset_t *regs = gregs;
861 struct gdbarch *gdbarch = get_regcache_arch (rc);
864 DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);
866 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
867 regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) ®s->pc);
868 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
869 regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) ®s->ps);
870 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
871 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
872 (char *) ®s->windowbase);
873 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
874 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
875 (char *) ®s->windowstart);
876 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
877 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
878 (char *) ®s->lbeg);
879 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
880 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
881 (char *) ®s->lend);
882 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
883 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
884 (char *) ®s->lcount);
885 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
886 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
887 (char *) ®s->sar);
888 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
889 && regnum < gdbarch_tdep (gdbarch)->ar_base
890 + gdbarch_tdep (gdbarch)->num_aregs)
891 regcache_raw_supply (rc, regnum,
892 (char *) ®s->ar[regnum - gdbarch_tdep
893 (gdbarch)->ar_base]);
894 else if (regnum == -1)
896 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
897 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
898 (char *) ®s->ar[i]);
903 /* Xtensa register set. */
909 xtensa_supply_gregset
913 /* Return the appropriate register set for the core
914 section identified by SECT_NAME and SECT_SIZE. */
916 static const struct regset *
917 xtensa_regset_from_core_section (struct gdbarch *core_arch,
918 const char *sect_name,
921 DEBUGTRACE ("xtensa_regset_from_core_section "
922 "(..., sect_name==\"%s\", sect_size==%x)\n",
923 sect_name, (unsigned int) sect_size);
925 if (strcmp (sect_name, ".reg") == 0
926 && sect_size >= sizeof(xtensa_elf_gregset_t))
927 return &xtensa_gregset;
933 /* Handling frames. */
935 /* Number of registers to save in case of Windowed ABI. */
936 #define XTENSA_NUM_SAVED_AREGS 12
938 /* Frame cache part for Windowed ABI. */
939 typedef struct xtensa_windowed_frame_cache
941 int wb; /* WINDOWBASE of the previous frame. */
942 int callsize; /* Call size of this frame. */
943 int ws; /* WINDOWSTART of the previous frame. It keeps track of
944 life windows only. If there is no bit set for the
945 window, that means it had been already spilled
946 because of window overflow. */
948 /* Addresses of spilled A-registers.
949 AREGS[i] == -1, if corresponding AR is alive. */
950 CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
951 } xtensa_windowed_frame_cache_t;
953 /* Call0 ABI Definitions. */
955 #define C0_MAXOPDS 3 /* Maximum number of operands for prologue
957 #define C0_NREGS 16 /* Number of A-registers to track. */
958 #define C0_CLESV 12 /* Callee-saved registers are here and up. */
959 #define C0_SP 1 /* Register used as SP. */
960 #define C0_FP 15 /* Register used as FP. */
961 #define C0_RA 0 /* Register used as return address. */
962 #define C0_ARGS 2 /* Register used as first arg/retval. */
963 #define C0_NARGS 6 /* Number of A-regs for args/retvals. */
965 /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
966 A-register where the current content of the reg came from (in terms
967 of an original reg and a constant). Negative values of c0_rt[n].fp_reg
968 mean that the orignal content of the register was saved to the stack.
969 c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
970 know where SP will end up until the entire prologue has been analyzed. */
972 #define C0_CONST -1 /* fr_reg value if register contains a constant. */
973 #define C0_INEXP -2 /* fr_reg value if inexpressible as reg + offset. */
974 #define C0_NOSTK -1 /* to_stk value if register has not been stored. */
976 extern xtensa_isa xtensa_default_isa;
978 typedef struct xtensa_c0reg
980 int fr_reg; /* original register from which register content
981 is derived, or C0_CONST, or C0_INEXP. */
982 int fr_ofs; /* constant offset from reg, or immediate value. */
983 int to_stk; /* offset from original SP to register (4-byte aligned),
984 or C0_NOSTK if register has not been saved. */
987 /* Frame cache part for Call0 ABI. */
988 typedef struct xtensa_call0_frame_cache
990 int c0_frmsz; /* Stack frame size. */
991 int c0_hasfp; /* Current frame uses frame pointer. */
992 int fp_regnum; /* A-register used as FP. */
993 int c0_fp; /* Actual value of frame pointer. */
994 int c0_fpalign; /* Dinamic adjustment for the stack
995 pointer. It's an AND mask. Zero,
996 if alignment was not adjusted. */
997 int c0_old_sp; /* In case of dynamic adjustment, it is
998 a register holding unaligned sp.
999 C0_INEXP, when undefined. */
1000 int c0_sp_ofs; /* If "c0_old_sp" was spilled it's a
1001 stack offset. C0_NOSTK otherwise. */
1003 xtensa_c0reg_t c0_rt[C0_NREGS]; /* Register tracking information. */
1004 } xtensa_call0_frame_cache_t;
1006 typedef struct xtensa_frame_cache
1008 CORE_ADDR base; /* Stack pointer of this frame. */
1009 CORE_ADDR pc; /* PC of this frame at the function entry point. */
1010 CORE_ADDR ra; /* The raw return address of this frame. */
1011 CORE_ADDR ps; /* The PS register of the previous (older) frame. */
1012 CORE_ADDR prev_sp; /* Stack Pointer of the previous (older) frame. */
1013 int call0; /* It's a call0 framework (else windowed). */
1016 xtensa_windowed_frame_cache_t wd; /* call0 == false. */
1017 xtensa_call0_frame_cache_t c0; /* call0 == true. */
1019 } xtensa_frame_cache_t;
1022 static struct xtensa_frame_cache *
1023 xtensa_alloc_frame_cache (int windowed)
1025 xtensa_frame_cache_t *cache;
1028 DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
1030 cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
1037 cache->call0 = !windowed;
1040 cache->c0.c0_frmsz = -1;
1041 cache->c0.c0_hasfp = 0;
1042 cache->c0.fp_regnum = -1;
1043 cache->c0.c0_fp = -1;
1044 cache->c0.c0_fpalign = 0;
1045 cache->c0.c0_old_sp = C0_INEXP;
1046 cache->c0.c0_sp_ofs = C0_NOSTK;
1048 for (i = 0; i < C0_NREGS; i++)
1050 cache->c0.c0_rt[i].fr_reg = i;
1051 cache->c0.c0_rt[i].fr_ofs = 0;
1052 cache->c0.c0_rt[i].to_stk = C0_NOSTK;
1059 cache->wd.callsize = -1;
1061 for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
1062 cache->wd.aregs[i] = -1;
1069 xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
1071 return address & ~15;
1076 xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1081 DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n",
1082 host_address_to_string (next_frame));
1084 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1085 pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1087 DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
1093 static struct frame_id
1094 xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1098 /* THIS-FRAME is a dummy frame. Return a frame ID of that frame. */
1100 pc = get_frame_pc (this_frame);
1101 fp = get_frame_register_unsigned
1102 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1104 /* Make dummy frame ID unique by adding a constant. */
1105 return frame_id_build (fp + SP_ALIGNMENT, pc);
1108 /* Returns true, if instruction to execute next is unique to Xtensa Window
1109 Interrupt Handlers. It can only be one of L32E, S32E, RFWO, or RFWU. */
1112 xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
1114 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1115 unsigned int insn = read_memory_integer (pc, 4, byte_order);
1118 if (byte_order == BFD_ENDIAN_BIG)
1120 /* Check, if this is L32E or S32E. */
1121 code = insn & 0xf000ff00;
1122 if ((code == 0x00009000) || (code == 0x00009400))
1124 /* Check, if this is RFWU or RFWO. */
1125 code = insn & 0xffffff00;
1126 return ((code == 0x00430000) || (code == 0x00530000));
1130 /* Check, if this is L32E or S32E. */
1131 code = insn & 0x00ff000f;
1132 if ((code == 0x090000) || (code == 0x490000))
1134 /* Check, if this is RFWU or RFWO. */
1135 code = insn & 0x00ffffff;
1136 return ((code == 0x00003400) || (code == 0x00003500));
1140 /* Returns the best guess about which register is a frame pointer
1141 for the function containing CURRENT_PC. */
1143 #define XTENSA_ISA_BSZ 32 /* Instruction buffer size. */
1144 #define XTENSA_ISA_BADPC ((CORE_ADDR)0) /* Bad PC value. */
1147 xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
1149 #define RETURN_FP goto done
1151 unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
1152 CORE_ADDR start_addr;
1154 xtensa_insnbuf ins, slot;
1155 gdb_byte ibuf[XTENSA_ISA_BSZ];
1156 CORE_ADDR ia, bt, ba;
1158 int ilen, islots, is;
1160 const char *opcname;
1162 find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
1163 if (start_addr == 0)
1166 if (!xtensa_default_isa)
1167 xtensa_default_isa = xtensa_isa_init (0, 0);
1168 isa = xtensa_default_isa;
1169 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
1170 ins = xtensa_insnbuf_alloc (isa);
1171 slot = xtensa_insnbuf_alloc (isa);
1174 for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
1176 if (ia + xtensa_isa_maxlength (isa) > bt)
1179 bt = (ba + XTENSA_ISA_BSZ) < current_pc
1180 ? ba + XTENSA_ISA_BSZ : current_pc;
1181 if (target_read_memory (ba, ibuf, bt - ba) != 0)
1185 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
1186 ifmt = xtensa_format_decode (isa, ins);
1187 if (ifmt == XTENSA_UNDEFINED)
1189 ilen = xtensa_format_length (isa, ifmt);
1190 if (ilen == XTENSA_UNDEFINED)
1192 islots = xtensa_format_num_slots (isa, ifmt);
1193 if (islots == XTENSA_UNDEFINED)
1196 for (is = 0; is < islots; ++is)
1198 if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
1201 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
1202 if (opc == XTENSA_UNDEFINED)
1205 opcname = xtensa_opcode_name (isa, opc);
1207 if (strcasecmp (opcname, "mov.n") == 0
1208 || strcasecmp (opcname, "or") == 0)
1210 unsigned int register_operand;
1212 /* Possible candidate for setting frame pointer
1213 from A1. This is what we are looking for. */
1215 if (xtensa_operand_get_field (isa, opc, 1, ifmt,
1216 is, slot, ®ister_operand) != 0)
1218 if (xtensa_operand_decode (isa, opc, 1, ®ister_operand) != 0)
1220 if (register_operand == 1) /* Mov{.n} FP A1. */
1222 if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
1223 ®ister_operand) != 0)
1225 if (xtensa_operand_decode (isa, opc, 0,
1226 ®ister_operand) != 0)
1230 = gdbarch_tdep (gdbarch)->a0_base + register_operand;
1236 /* We have problems decoding the memory. */
1238 || strcasecmp (opcname, "ill") == 0
1239 || strcasecmp (opcname, "ill.n") == 0
1240 /* Hit planted breakpoint. */
1241 || strcasecmp (opcname, "break") == 0
1242 || strcasecmp (opcname, "break.n") == 0
1243 /* Flow control instructions finish prologue. */
1244 || xtensa_opcode_is_branch (isa, opc) > 0
1245 || xtensa_opcode_is_jump (isa, opc) > 0
1246 || xtensa_opcode_is_loop (isa, opc) > 0
1247 || xtensa_opcode_is_call (isa, opc) > 0
1248 || strcasecmp (opcname, "simcall") == 0
1249 || strcasecmp (opcname, "syscall") == 0)
1250 /* Can not continue analysis. */
1255 xtensa_insnbuf_free(isa, slot);
1256 xtensa_insnbuf_free(isa, ins);
1260 /* The key values to identify the frame using "cache" are
1262 cache->base = SP (or best guess about FP) of this frame;
1263 cache->pc = entry-PC (entry point of the frame function);
1264 cache->prev_sp = SP of the previous frame. */
1267 call0_frame_cache (struct frame_info *this_frame,
1268 xtensa_frame_cache_t *cache, CORE_ADDR pc);
1271 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
1272 xtensa_frame_cache_t *cache,
1275 static struct xtensa_frame_cache *
1276 xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
1278 xtensa_frame_cache_t *cache;
1279 CORE_ADDR ra, wb, ws, pc, sp, ps;
1280 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1281 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1282 unsigned int fp_regnum;
1283 int windowed, ps_regnum;
1288 pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
1289 ps_regnum = gdbarch_ps_regnum (gdbarch);
1290 ps = (ps_regnum >= 0
1291 ? get_frame_register_unsigned (this_frame, ps_regnum) : TX_PS);
1293 windowed = windowing_enabled (gdbarch, ps);
1295 /* Get pristine xtensa-frame. */
1296 cache = xtensa_alloc_frame_cache (windowed);
1297 *this_cache = cache;
1303 /* Get WINDOWBASE, WINDOWSTART, and PS registers. */
1304 wb = get_frame_register_unsigned (this_frame,
1305 gdbarch_tdep (gdbarch)->wb_regnum);
1306 ws = get_frame_register_unsigned (this_frame,
1307 gdbarch_tdep (gdbarch)->ws_regnum);
1309 op1 = read_memory_integer (pc, 1, byte_order);
1310 if (XTENSA_IS_ENTRY (gdbarch, op1))
1312 int callinc = CALLINC (ps);
1313 ra = get_frame_register_unsigned
1314 (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
1316 /* ENTRY hasn't been executed yet, therefore callsize is still 0. */
1317 cache->wd.callsize = 0;
1320 cache->prev_sp = get_frame_register_unsigned
1321 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1323 /* This only can be the outermost frame since we are
1324 just about to execute ENTRY. SP hasn't been set yet.
1325 We can assume any frame size, because it does not
1326 matter, and, let's fake frame base in cache. */
1327 cache->base = cache->prev_sp - 16;
1330 cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1331 cache->ps = (ps & ~PS_CALLINC_MASK)
1332 | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1338 fp_regnum = xtensa_scan_prologue (gdbarch, pc);
1339 ra = get_frame_register_unsigned (this_frame,
1340 gdbarch_tdep (gdbarch)->a0_base);
1341 cache->wd.callsize = WINSIZE (ra);
1342 cache->wd.wb = (wb - cache->wd.callsize / 4)
1343 & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
1344 cache->wd.ws = ws & ~(1 << wb);
1346 cache->pc = get_frame_func (this_frame);
1347 cache->ra = (pc & 0xc0000000) | (ra & 0x3fffffff);
1348 cache->ps = (ps & ~PS_CALLINC_MASK)
1349 | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1352 if (cache->wd.ws == 0)
1357 sp = get_frame_register_unsigned
1358 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
1360 for (i = 0; i < 4; i++, sp += 4)
1362 cache->wd.aregs[i] = sp;
1365 if (cache->wd.callsize > 4)
1367 /* Set A4...A7/A11. */
1368 /* Get the SP of the frame previous to the previous one.
1369 To achieve this, we have to dereference SP twice. */
1370 sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
1371 sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
1372 sp -= cache->wd.callsize * 4;
1374 for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
1376 cache->wd.aregs[i] = sp;
1381 if ((cache->prev_sp == 0) && ( ra != 0 ))
1382 /* If RA is equal to 0 this frame is an outermost frame. Leave
1383 cache->prev_sp unchanged marking the boundary of the frame stack. */
1385 if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
1387 /* Register window overflow already happened.
1388 We can read caller's SP from the proper spill loction. */
1389 sp = get_frame_register_unsigned
1390 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1391 cache->prev_sp = read_memory_integer (sp - 12, 4, byte_order);
1395 /* Read caller's frame SP directly from the previous window. */
1396 int regnum = arreg_number
1397 (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
1400 cache->prev_sp = xtensa_read_register (regnum);
1404 else if (xtensa_window_interrupt_insn (gdbarch, pc))
1406 /* Execution stopped inside Xtensa Window Interrupt Handler. */
1408 xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
1409 /* Everything was set already, including cache->base. */
1412 else /* Call0 framework. */
1414 call0_frame_cache (this_frame, cache, pc);
1415 fp_regnum = cache->c0.fp_regnum;
1418 cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
1423 static int xtensa_session_once_reported = 1;
1425 /* Report a problem with prologue analysis while doing backtracing.
1426 But, do it only once to avoid annoyng repeated messages. */
1431 if (xtensa_session_once_reported == 0)
1433 \nUnrecognised function prologue. Stack trace cannot be resolved. \
1434 This message will not be repeated in this session.\n"));
1436 xtensa_session_once_reported = 1;
1441 xtensa_frame_this_id (struct frame_info *this_frame,
1443 struct frame_id *this_id)
1445 struct xtensa_frame_cache *cache =
1446 xtensa_frame_cache (this_frame, this_cache);
1448 if (cache->prev_sp == 0)
1451 (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
1454 static struct value *
1455 xtensa_frame_prev_register (struct frame_info *this_frame,
1459 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1460 struct xtensa_frame_cache *cache;
1461 ULONGEST saved_reg = 0;
1464 if (*this_cache == NULL)
1465 *this_cache = xtensa_frame_cache (this_frame, this_cache);
1466 cache = *this_cache;
1468 if (regnum ==gdbarch_pc_regnum (gdbarch))
1469 saved_reg = cache->ra;
1470 else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
1471 saved_reg = cache->prev_sp;
1472 else if (!cache->call0)
1474 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
1475 saved_reg = cache->wd.ws;
1476 else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
1477 saved_reg = cache->wd.wb;
1478 else if (regnum == gdbarch_ps_regnum (gdbarch))
1479 saved_reg = cache->ps;
1487 return frame_unwind_got_constant (this_frame, regnum, saved_reg);
1489 if (!cache->call0) /* Windowed ABI. */
1491 /* Convert A-register numbers to AR-register numbers,
1492 if we deal with A-register. */
1493 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
1494 && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
1495 regnum = arreg_number (gdbarch, regnum, cache->wd.wb);
1497 /* Check, if we deal with AR-register saved on stack. */
1498 if (regnum >= gdbarch_tdep (gdbarch)->ar_base
1499 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1500 + gdbarch_tdep (gdbarch)->num_aregs))
1502 int areg = areg_number (gdbarch, regnum, cache->wd.wb);
1505 && areg < XTENSA_NUM_SAVED_AREGS
1506 && cache->wd.aregs[areg] != -1)
1507 return frame_unwind_got_memory (this_frame, regnum,
1508 cache->wd.aregs[areg]);
1511 else /* Call0 ABI. */
1513 int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
1514 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1516 ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
1523 /* If register was saved in the prologue, retrieve it. */
1524 stkofs = cache->c0.c0_rt[reg].to_stk;
1525 if (stkofs != C0_NOSTK)
1527 /* Determine SP on entry based on FP. */
1528 spe = cache->c0.c0_fp
1529 - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1531 return frame_unwind_got_memory (this_frame, regnum,
1537 /* All other registers have been either saved to
1538 the stack or are still alive in the processor. */
1540 return frame_unwind_got_register (this_frame, regnum, regnum);
1544 static const struct frame_unwind
1548 default_frame_unwind_stop_reason,
1549 xtensa_frame_this_id,
1550 xtensa_frame_prev_register,
1552 default_frame_sniffer
1556 xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
1558 struct xtensa_frame_cache *cache =
1559 xtensa_frame_cache (this_frame, this_cache);
1564 static const struct frame_base
1568 xtensa_frame_base_address,
1569 xtensa_frame_base_address,
1570 xtensa_frame_base_address
1575 xtensa_extract_return_value (struct type *type,
1576 struct regcache *regcache,
1579 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1580 bfd_byte *valbuf = dst;
1581 int len = TYPE_LENGTH (type);
1586 DEBUGTRACE ("xtensa_extract_return_value (...)\n");
1588 gdb_assert(len > 0);
1590 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1592 /* First, we have to find the caller window in the register file. */
1593 regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1594 callsize = extract_call_winsize (gdbarch, pc);
1596 /* On Xtensa, we can return up to 4 words (or 2 for call12). */
1597 if (len > (callsize > 8 ? 8 : 16))
1598 internal_error (__FILE__, __LINE__,
1599 _("cannot extract return value of %d bytes long"),
1602 /* Get the register offset of the return
1603 register (A2) in the caller window. */
1604 regcache_raw_read_unsigned
1605 (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1606 areg = arreg_number (gdbarch,
1607 gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1611 /* No windowing hardware - Call0 ABI. */
1612 areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1615 DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
1617 if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1620 for (; len > 0; len -= 4, areg++, valbuf += 4)
1623 regcache_raw_read_part (regcache, areg, offset, len, valbuf);
1625 regcache_raw_read (regcache, areg, valbuf);
1631 xtensa_store_return_value (struct type *type,
1632 struct regcache *regcache,
1635 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1636 const bfd_byte *valbuf = dst;
1640 int len = TYPE_LENGTH (type);
1643 DEBUGTRACE ("xtensa_store_return_value (...)\n");
1645 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1647 regcache_raw_read_unsigned
1648 (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1649 regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1650 callsize = extract_call_winsize (gdbarch, pc);
1652 if (len > (callsize > 8 ? 8 : 16))
1653 internal_error (__FILE__, __LINE__,
1654 _("unimplemented for this length: %d"),
1655 TYPE_LENGTH (type));
1656 areg = arreg_number (gdbarch,
1657 gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1659 DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
1660 callsize, (int) wb);
1664 areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1667 if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1670 for (; len > 0; len -= 4, areg++, valbuf += 4)
1673 regcache_raw_write_part (regcache, areg, offset, len, valbuf);
1675 regcache_raw_write (regcache, areg, valbuf);
1680 static enum return_value_convention
1681 xtensa_return_value (struct gdbarch *gdbarch,
1682 struct value *function,
1683 struct type *valtype,
1684 struct regcache *regcache,
1686 const gdb_byte *writebuf)
1688 /* Structures up to 16 bytes are returned in registers. */
1690 int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1691 || TYPE_CODE (valtype) == TYPE_CODE_UNION
1692 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1693 && TYPE_LENGTH (valtype) > 16);
1696 return RETURN_VALUE_STRUCT_CONVENTION;
1698 DEBUGTRACE ("xtensa_return_value(...)\n");
1700 if (writebuf != NULL)
1702 xtensa_store_return_value (valtype, regcache, writebuf);
1705 if (readbuf != NULL)
1707 gdb_assert (!struct_return);
1708 xtensa_extract_return_value (valtype, regcache, readbuf);
1710 return RETURN_VALUE_REGISTER_CONVENTION;
1717 xtensa_push_dummy_call (struct gdbarch *gdbarch,
1718 struct value *function,
1719 struct regcache *regcache,
1722 struct value **args,
1725 CORE_ADDR struct_addr)
1727 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1729 int size, onstack_size;
1730 gdb_byte *buf = (gdb_byte *) alloca (16);
1732 struct argument_info
1734 const bfd_byte *contents;
1736 int onstack; /* onstack == 0 => in reg */
1737 int align; /* alignment */
1740 int offset; /* stack offset if on stack. */
1741 int regno; /* regno if in register. */
1745 struct argument_info *arg_info =
1746 (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
1750 DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
1752 if (xtensa_debug_level > 3)
1755 DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
1756 DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
1757 "struct_addr=0x%x\n",
1758 (int) sp, (int) struct_return, (int) struct_addr);
1760 for (i = 0; i < nargs; i++)
1762 struct value *arg = args[i];
1763 struct type *arg_type = check_typedef (value_type (arg));
1764 fprintf_unfiltered (gdb_stdlog, "%2d: %s %3d ", i,
1765 host_address_to_string (arg),
1766 TYPE_LENGTH (arg_type));
1767 switch (TYPE_CODE (arg_type))
1770 fprintf_unfiltered (gdb_stdlog, "int");
1772 case TYPE_CODE_STRUCT:
1773 fprintf_unfiltered (gdb_stdlog, "struct");
1776 fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
1779 fprintf_unfiltered (gdb_stdlog, " %s\n",
1780 host_address_to_string (value_contents (arg)));
1784 /* First loop: collect information.
1785 Cast into type_long. (This shouldn't happen often for C because
1786 GDB already does this earlier.) It's possible that GDB could
1787 do it all the time but it's harmless to leave this code here. */
1794 size = REGISTER_SIZE;
1796 for (i = 0; i < nargs; i++)
1798 struct argument_info *info = &arg_info[i];
1799 struct value *arg = args[i];
1800 struct type *arg_type = check_typedef (value_type (arg));
1802 switch (TYPE_CODE (arg_type))
1805 case TYPE_CODE_BOOL:
1806 case TYPE_CODE_CHAR:
1807 case TYPE_CODE_RANGE:
1808 case TYPE_CODE_ENUM:
1810 /* Cast argument to long if necessary as the mask does it too. */
1811 if (TYPE_LENGTH (arg_type)
1812 < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
1814 arg_type = builtin_type (gdbarch)->builtin_long;
1815 arg = value_cast (arg_type, arg);
1817 /* Aligment is equal to the type length for the basic types. */
1818 info->align = TYPE_LENGTH (arg_type);
1823 /* Align doubles correctly. */
1824 if (TYPE_LENGTH (arg_type)
1825 == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
1826 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
1828 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1831 case TYPE_CODE_STRUCT:
1833 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1836 info->length = TYPE_LENGTH (arg_type);
1837 info->contents = value_contents (arg);
1839 /* Align size and onstack_size. */
1840 size = (size + info->align - 1) & ~(info->align - 1);
1841 onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
1843 if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
1846 info->u.offset = onstack_size;
1847 onstack_size += info->length;
1852 info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
1854 size += info->length;
1857 /* Adjust the stack pointer and align it. */
1858 sp = align_down (sp - onstack_size, SP_ALIGNMENT);
1860 /* Simulate MOVSP, if Windowed ABI. */
1861 if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1864 read_memory (osp - 16, buf, 16);
1865 write_memory (sp - 16, buf, 16);
1868 /* Second Loop: Load arguments. */
1872 store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
1873 regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
1876 for (i = 0; i < nargs; i++)
1878 struct argument_info *info = &arg_info[i];
1882 int n = info->length;
1883 CORE_ADDR offset = sp + info->u.offset;
1885 /* Odd-sized structs are aligned to the lower side of a memory
1886 word in big-endian mode and require a shift. This only
1887 applies for structures smaller than one word. */
1889 if (n < REGISTER_SIZE
1890 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1891 offset += (REGISTER_SIZE - n);
1893 write_memory (offset, info->contents, info->length);
1898 int n = info->length;
1899 const bfd_byte *cp = info->contents;
1900 int r = info->u.regno;
1902 /* Odd-sized structs are aligned to the lower side of registers in
1903 big-endian mode and require a shift. The odd-sized leftover will
1904 be at the end. Note that this is only true for structures smaller
1905 than REGISTER_SIZE; for larger odd-sized structures the excess
1906 will be left-aligned in the register on both endiannesses. */
1908 if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
1911 v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
1912 v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
1914 store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
1915 regcache_cooked_write (regcache, r, buf);
1917 cp += REGISTER_SIZE;
1924 regcache_cooked_write (regcache, r, cp);
1926 cp += REGISTER_SIZE;
1933 /* Set the return address of dummy frame to the dummy address.
1934 The return address for the current function (in A0) is
1935 saved in the dummy frame, so we can savely overwrite A0 here. */
1937 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1941 ra = (bp_addr & 0x3fffffff) | 0x40000000;
1942 regcache_raw_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), &val);
1943 ps = (unsigned long) val & ~0x00030000;
1944 regcache_cooked_write_unsigned
1945 (regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
1946 regcache_cooked_write_unsigned (regcache,
1947 gdbarch_ps_regnum (gdbarch),
1950 /* All the registers have been saved. After executing
1951 dummy call, they all will be restored. So it's safe
1952 to modify WINDOWSTART register to make it look like there
1953 is only one register window corresponding to WINDOWEBASE. */
1955 regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
1956 regcache_cooked_write_unsigned
1957 (regcache, gdbarch_tdep (gdbarch)->ws_regnum,
1958 1 << extract_unsigned_integer (buf, 4, byte_order));
1962 /* Simulate CALL0: write RA into A0 register. */
1963 regcache_cooked_write_unsigned
1964 (regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
1967 /* Set new stack pointer and return it. */
1968 regcache_cooked_write_unsigned (regcache,
1969 gdbarch_tdep (gdbarch)->a0_base + 1, sp);
1970 /* Make dummy frame ID unique by adding a constant. */
1971 return sp + SP_ALIGNMENT;
1975 /* Return a breakpoint for the current location of PC. We always use
1976 the density version if we have density instructions (regardless of the
1977 current instruction at PC), and use regular instructions otherwise. */
1979 #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
1980 #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
1981 #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
1982 #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
1984 static const unsigned char *
1985 xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1988 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
1989 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
1990 static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
1991 static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;
1993 DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
1995 if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
1997 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1999 *lenptr = sizeof (density_big_breakpoint);
2000 return density_big_breakpoint;
2004 *lenptr = sizeof (density_little_breakpoint);
2005 return density_little_breakpoint;
2010 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2012 *lenptr = sizeof (big_breakpoint);
2013 return big_breakpoint;
2017 *lenptr = sizeof (little_breakpoint);
2018 return little_breakpoint;
2023 /* Call0 ABI support routines. */
2025 /* Return true, if PC points to "ret" or "ret.n". */
2028 call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
2030 #define RETURN_RET goto done
2032 xtensa_insnbuf ins, slot;
2033 gdb_byte ibuf[XTENSA_ISA_BSZ];
2034 CORE_ADDR ia, bt, ba;
2036 int ilen, islots, is;
2038 const char *opcname;
2041 isa = xtensa_default_isa;
2042 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2043 ins = xtensa_insnbuf_alloc (isa);
2044 slot = xtensa_insnbuf_alloc (isa);
2047 for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
2049 if (ia + xtensa_isa_maxlength (isa) > bt)
2052 bt = (ba + XTENSA_ISA_BSZ) < finish_pc
2053 ? ba + XTENSA_ISA_BSZ : finish_pc;
2054 if (target_read_memory (ba, ibuf, bt - ba) != 0 )
2058 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2059 ifmt = xtensa_format_decode (isa, ins);
2060 if (ifmt == XTENSA_UNDEFINED)
2062 ilen = xtensa_format_length (isa, ifmt);
2063 if (ilen == XTENSA_UNDEFINED)
2065 islots = xtensa_format_num_slots (isa, ifmt);
2066 if (islots == XTENSA_UNDEFINED)
2069 for (is = 0; is < islots; ++is)
2071 if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
2074 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2075 if (opc == XTENSA_UNDEFINED)
2078 opcname = xtensa_opcode_name (isa, opc);
2080 if ((strcasecmp (opcname, "ret.n") == 0)
2081 || (strcasecmp (opcname, "ret") == 0))
2089 xtensa_insnbuf_free(isa, slot);
2090 xtensa_insnbuf_free(isa, ins);
2094 /* Call0 opcode class. Opcodes are preclassified according to what they
2095 mean for Call0 prologue analysis, and their number of significant operands.
2096 The purpose of this is to simplify prologue analysis by separating
2097 instruction decoding (libisa) from the semantics of prologue analysis. */
2101 c0opc_illegal, /* Unknown to libisa (invalid) or 'ill' opcode. */
2102 c0opc_uninteresting, /* Not interesting for Call0 prologue analysis. */
2103 c0opc_flow, /* Flow control insn. */
2104 c0opc_entry, /* ENTRY indicates non-Call0 prologue. */
2105 c0opc_break, /* Debugger software breakpoints. */
2106 c0opc_add, /* Adding two registers. */
2107 c0opc_addi, /* Adding a register and an immediate. */
2108 c0opc_and, /* Bitwise "and"-ing two registers. */
2109 c0opc_sub, /* Subtracting a register from a register. */
2110 c0opc_mov, /* Moving a register to a register. */
2111 c0opc_movi, /* Moving an immediate to a register. */
2112 c0opc_l32r, /* Loading a literal. */
2113 c0opc_s32i, /* Storing word at fixed offset from a base register. */
2114 c0opc_rwxsr, /* RSR, WRS, or XSR instructions. */
2115 c0opc_l32e, /* L32E instruction. */
2116 c0opc_s32e, /* S32E instruction. */
2117 c0opc_rfwo, /* RFWO instruction. */
2118 c0opc_rfwu, /* RFWU instruction. */
2119 c0opc_NrOf /* Number of opcode classifications. */
2122 /* Return true, if OPCNAME is RSR, WRS, or XSR instruction. */
2125 rwx_special_register (const char *opcname)
2127 char ch = *opcname++;
2129 if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
2131 if (*opcname++ != 's')
2133 if (*opcname++ != 'r')
2135 if (*opcname++ != '.')
2141 /* Classify an opcode based on what it means for Call0 prologue analysis. */
2143 static xtensa_insn_kind
2144 call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
2146 const char *opcname;
2147 xtensa_insn_kind opclass = c0opc_uninteresting;
2149 DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
2151 /* Get opcode name and handle special classifications. */
2153 opcname = xtensa_opcode_name (isa, opc);
2156 || strcasecmp (opcname, "ill") == 0
2157 || strcasecmp (opcname, "ill.n") == 0)
2158 opclass = c0opc_illegal;
2159 else if (strcasecmp (opcname, "break") == 0
2160 || strcasecmp (opcname, "break.n") == 0)
2161 opclass = c0opc_break;
2162 else if (strcasecmp (opcname, "entry") == 0)
2163 opclass = c0opc_entry;
2164 else if (strcasecmp (opcname, "rfwo") == 0)
2165 opclass = c0opc_rfwo;
2166 else if (strcasecmp (opcname, "rfwu") == 0)
2167 opclass = c0opc_rfwu;
2168 else if (xtensa_opcode_is_branch (isa, opc) > 0
2169 || xtensa_opcode_is_jump (isa, opc) > 0
2170 || xtensa_opcode_is_loop (isa, opc) > 0
2171 || xtensa_opcode_is_call (isa, opc) > 0
2172 || strcasecmp (opcname, "simcall") == 0
2173 || strcasecmp (opcname, "syscall") == 0)
2174 opclass = c0opc_flow;
2176 /* Also, classify specific opcodes that need to be tracked. */
2177 else if (strcasecmp (opcname, "add") == 0
2178 || strcasecmp (opcname, "add.n") == 0)
2179 opclass = c0opc_add;
2180 else if (strcasecmp (opcname, "and") == 0)
2181 opclass = c0opc_and;
2182 else if (strcasecmp (opcname, "addi") == 0
2183 || strcasecmp (opcname, "addi.n") == 0
2184 || strcasecmp (opcname, "addmi") == 0)
2185 opclass = c0opc_addi;
2186 else if (strcasecmp (opcname, "sub") == 0)
2187 opclass = c0opc_sub;
2188 else if (strcasecmp (opcname, "mov.n") == 0
2189 || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro. */
2190 opclass = c0opc_mov;
2191 else if (strcasecmp (opcname, "movi") == 0
2192 || strcasecmp (opcname, "movi.n") == 0)
2193 opclass = c0opc_movi;
2194 else if (strcasecmp (opcname, "l32r") == 0)
2195 opclass = c0opc_l32r;
2196 else if (strcasecmp (opcname, "s32i") == 0
2197 || strcasecmp (opcname, "s32i.n") == 0)
2198 opclass = c0opc_s32i;
2199 else if (strcasecmp (opcname, "l32e") == 0)
2200 opclass = c0opc_l32e;
2201 else if (strcasecmp (opcname, "s32e") == 0)
2202 opclass = c0opc_s32e;
2203 else if (rwx_special_register (opcname))
2204 opclass = c0opc_rwxsr;
2209 /* Tracks register movement/mutation for a given operation, which may
2210 be within a bundle. Updates the destination register tracking info
2211 accordingly. The pc is needed only for pc-relative load instructions
2212 (eg. l32r). The SP register number is needed to identify stores to
2213 the stack frame. Returns 0, if analysis was succesfull, non-zero
2217 call0_track_op (struct gdbarch *gdbarch, xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
2218 xtensa_insn_kind opclass, int nods, unsigned odv[],
2219 CORE_ADDR pc, int spreg, xtensa_frame_cache_t *cache)
2221 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2222 unsigned litbase, litaddr, litval;
2227 /* 3 operands: dst, src, imm. */
2228 gdb_assert (nods == 3);
2229 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2230 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
2233 /* 3 operands: dst, src1, src2. */
2234 gdb_assert (nods == 3);
2235 if (src[odv[1]].fr_reg == C0_CONST)
2237 dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
2238 dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
2240 else if (src[odv[2]].fr_reg == C0_CONST)
2242 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2243 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
2245 else dst[odv[0]].fr_reg = C0_INEXP;
2248 /* 3 operands: dst, src1, src2. */
2249 gdb_assert (nods == 3);
2250 if (cache->c0.c0_fpalign == 0)
2252 /* Handle dynamic stack alignment. */
2253 if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
2255 if (src[odv[2]].fr_reg == C0_CONST)
2256 cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
2259 else if ((src[odv[0]].fr_reg == spreg)
2260 && (src[odv[2]].fr_reg == spreg))
2262 if (src[odv[1]].fr_reg == C0_CONST)
2263 cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
2266 /* else fall through. */
2268 if (src[odv[1]].fr_reg == C0_CONST)
2270 dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
2271 dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs & src[odv[1]].fr_ofs;
2273 else if (src[odv[2]].fr_reg == C0_CONST)
2275 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2276 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs & src[odv[2]].fr_ofs;
2278 else dst[odv[0]].fr_reg = C0_INEXP;
2281 /* 3 operands: dst, src1, src2. */
2282 gdb_assert (nods == 3);
2283 if (src[odv[2]].fr_reg == C0_CONST)
2285 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2286 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
2288 else dst[odv[0]].fr_reg = C0_INEXP;
2291 /* 2 operands: dst, src [, src]. */
2292 gdb_assert (nods == 2);
2293 /* First, check if it's a special case of saving unaligned SP
2294 to a spare register in case of dynamic stack adjustment.
2295 But, only do it one time. The second time could be initializing
2296 frame pointer. We don't want to overwrite the first one. */
2297 if ((odv[1] == spreg) && (cache->c0.c0_old_sp == C0_INEXP))
2298 cache->c0.c0_old_sp = odv[0];
2300 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2301 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
2304 /* 2 operands: dst, imm. */
2305 gdb_assert (nods == 2);
2306 dst[odv[0]].fr_reg = C0_CONST;
2307 dst[odv[0]].fr_ofs = odv[1];
2310 /* 2 operands: dst, literal offset. */
2311 gdb_assert (nods == 2);
2312 /* litbase = xtensa_get_litbase (pc); can be also used. */
2313 litbase = (gdbarch_tdep (gdbarch)->litbase_regnum == -1)
2314 ? 0 : xtensa_read_register
2315 (gdbarch_tdep (gdbarch)->litbase_regnum);
2316 litaddr = litbase & 1
2317 ? (litbase & ~1) + (signed)odv[1]
2318 : (pc + 3 + (signed)odv[1]) & ~3;
2319 litval = read_memory_integer (litaddr, 4, byte_order);
2320 dst[odv[0]].fr_reg = C0_CONST;
2321 dst[odv[0]].fr_ofs = litval;
2324 /* 3 operands: value, base, offset. */
2325 gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
2326 /* First, check if it's a spill for saved unaligned SP,
2327 when dynamic stack adjustment was applied to this frame. */
2328 if ((cache->c0.c0_fpalign != 0) /* Dynamic stack adjustment. */
2329 && (odv[1] == spreg) /* SP usage indicates spill. */
2330 && (odv[0] == cache->c0.c0_old_sp)) /* Old SP register spilled. */
2331 cache->c0.c0_sp_ofs = odv[2];
2333 if (src[odv[1]].fr_reg == spreg /* Store to stack frame. */
2334 && (src[odv[1]].fr_ofs & 3) == 0 /* Alignment preserved. */
2335 && src[odv[0]].fr_reg >= 0 /* Value is from a register. */
2336 && src[odv[0]].fr_ofs == 0 /* Value hasn't been modified. */
2337 && src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time. */
2339 /* ISA encoding guarantees alignment. But, check it anyway. */
2340 gdb_assert ((odv[2] & 3) == 0);
2341 dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
2344 /* If we end up inside Window Overflow / Underflow interrupt handler
2345 report an error because these handlers should have been handled
2346 already in a different way. */
2358 /* Analyze prologue of the function at start address to determine if it uses
2359 the Call0 ABI, and if so track register moves and linear modifications
2360 in the prologue up to the PC or just beyond the prologue, whichever is
2361 first. An 'entry' instruction indicates non-Call0 ABI and the end of the
2362 prologue. The prologue may overlap non-prologue instructions but is
2363 guaranteed to end by the first flow-control instruction (jump, branch,
2364 call or return). Since an optimized function may move information around
2365 and change the stack frame arbitrarily during the prologue, the information
2366 is guaranteed valid only at the point in the function indicated by the PC.
2367 May be used to skip the prologue or identify the ABI, w/o tracking.
2369 Returns: Address of first instruction after prologue, or PC (whichever
2370 is first), or 0, if decoding failed (in libisa).
2372 start Start address of function/prologue.
2373 pc Program counter to stop at. Use 0 to continue to end of prologue.
2374 If 0, avoids infinite run-on in corrupt code memory by bounding
2375 the scan to the end of the function if that can be determined.
2376 nregs Number of general registers to track.
2378 cache Xtensa frame cache.
2380 Note that these may produce useful results even if decoding fails
2381 because they begin with default assumptions that analysis may change. */
2384 call0_analyze_prologue (struct gdbarch *gdbarch,
2385 CORE_ADDR start, CORE_ADDR pc,
2386 int nregs, xtensa_frame_cache_t *cache)
2388 CORE_ADDR ia; /* Current insn address in prologue. */
2389 CORE_ADDR ba = 0; /* Current address at base of insn buffer. */
2390 CORE_ADDR bt; /* Current address at top+1 of insn buffer. */
2391 gdb_byte ibuf[XTENSA_ISA_BSZ];/* Instruction buffer for decoding prologue. */
2392 xtensa_isa isa; /* libisa ISA handle. */
2393 xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot. */
2394 xtensa_format ifmt; /* libisa instruction format. */
2395 int ilen, islots, is; /* Instruction length, nbr slots, current slot. */
2396 xtensa_opcode opc; /* Opcode in current slot. */
2397 xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis. */
2398 int nods; /* Opcode number of operands. */
2399 unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa. */
2400 xtensa_c0reg_t *rtmp; /* Register tracking info snapshot. */
2401 int j; /* General loop counter. */
2402 int fail = 0; /* Set non-zero and exit, if decoding fails. */
2403 CORE_ADDR body_pc; /* The PC for the first non-prologue insn. */
2404 CORE_ADDR end_pc; /* The PC for the lust function insn. */
2406 struct symtab_and_line prologue_sal;
2408 DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
2409 (int)start, (int)pc);
2411 /* Try to limit the scan to the end of the function if a non-zero pc
2412 arg was not supplied to avoid probing beyond the end of valid memory.
2413 If memory is full of garbage that classifies as c0opc_uninteresting.
2414 If this fails (eg. if no symbols) pc ends up 0 as it was.
2415 Intialize the Call0 frame and register tracking info.
2416 Assume it's Call0 until an 'entry' instruction is encountered.
2417 Assume we may be in the prologue until we hit a flow control instr. */
2423 /* Find out, if we have an information about the prologue from DWARF. */
2424 prologue_sal = find_pc_line (start, 0);
2425 if (prologue_sal.line != 0) /* Found debug info. */
2426 body_pc = prologue_sal.end;
2428 /* If we are going to analyze the prologue in general without knowing about
2429 the current PC, make the best assumtion for the end of the prologue. */
2432 find_pc_partial_function (start, 0, NULL, &end_pc);
2433 body_pc = min (end_pc, body_pc);
2436 body_pc = min (pc, body_pc);
2439 rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
2441 if (!xtensa_default_isa)
2442 xtensa_default_isa = xtensa_isa_init (0, 0);
2443 isa = xtensa_default_isa;
2444 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2445 ins = xtensa_insnbuf_alloc (isa);
2446 slot = xtensa_insnbuf_alloc (isa);
2448 for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
2450 /* (Re)fill instruction buffer from memory if necessary, but do not
2451 read memory beyond PC to be sure we stay within text section
2452 (this protection only works if a non-zero pc is supplied). */
2454 if (ia + xtensa_isa_maxlength (isa) > bt)
2457 bt = (ba + XTENSA_ISA_BSZ) < body_pc ? ba + XTENSA_ISA_BSZ : body_pc;
2458 if (target_read_memory (ba, ibuf, bt - ba) != 0 )
2459 error (_("Unable to read target memory ..."));
2462 /* Decode format information. */
2464 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2465 ifmt = xtensa_format_decode (isa, ins);
2466 if (ifmt == XTENSA_UNDEFINED)
2471 ilen = xtensa_format_length (isa, ifmt);
2472 if (ilen == XTENSA_UNDEFINED)
2477 islots = xtensa_format_num_slots (isa, ifmt);
2478 if (islots == XTENSA_UNDEFINED)
2484 /* Analyze a bundle or a single instruction, using a snapshot of
2485 the register tracking info as input for the entire bundle so that
2486 register changes do not take effect within this bundle. */
2488 for (j = 0; j < nregs; ++j)
2489 rtmp[j] = cache->c0.c0_rt[j];
2491 for (is = 0; is < islots; ++is)
2493 /* Decode a slot and classify the opcode. */
2495 fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
2499 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2500 DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
2502 if (opc == XTENSA_UNDEFINED)
2503 opclass = c0opc_illegal;
2505 opclass = call0_classify_opcode (isa, opc);
2507 /* Decide whether to track this opcode, ignore it, or bail out. */
2516 case c0opc_uninteresting:
2519 case c0opc_flow: /* Flow control instructions stop analysis. */
2520 case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis. */
2525 ia += ilen; /* Skip over 'entry' insn. */
2532 /* Only expected opcodes should get this far. */
2534 /* Extract and decode the operands. */
2535 nods = xtensa_opcode_num_operands (isa, opc);
2536 if (nods == XTENSA_UNDEFINED)
2542 for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
2544 fail = xtensa_operand_get_field (isa, opc, j, ifmt,
2549 fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
2554 /* Check operands to verify use of 'mov' assembler macro. */
2555 if (opclass == c0opc_mov && nods == 3)
2557 if (odv[2] == odv[1])
2560 if ((odv[0] == 1) && (odv[1] != 1))
2561 /* OR A1, An, An , where n != 1.
2562 This means we are inside epilogue already. */
2567 opclass = c0opc_uninteresting;
2572 /* Track register movement and modification for this operation. */
2573 fail = call0_track_op (gdbarch, cache->c0.c0_rt, rtmp,
2574 opclass, nods, odv, ia, 1, cache);
2580 DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
2581 (unsigned)ia, fail ? "failed" : "succeeded");
2582 xtensa_insnbuf_free(isa, slot);
2583 xtensa_insnbuf_free(isa, ins);
2584 return fail ? XTENSA_ISA_BADPC : ia;
2587 /* Initialize frame cache for the current frame in CALL0 ABI. */
2590 call0_frame_cache (struct frame_info *this_frame,
2591 xtensa_frame_cache_t *cache, CORE_ADDR pc)
2593 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2594 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2595 CORE_ADDR start_pc; /* The beginning of the function. */
2596 CORE_ADDR body_pc=UINT_MAX; /* PC, where prologue analysis stopped. */
2597 CORE_ADDR sp, fp, ra;
2598 int fp_regnum = C0_SP, c0_hasfp = 0, c0_frmsz = 0, prev_sp = 0, to_stk;
2600 sp = get_frame_register_unsigned
2601 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
2602 fp = sp; /* Assume FP == SP until proven otherwise. */
2604 /* Find the beginning of the prologue of the function containing the PC
2605 and analyze it up to the PC or the end of the prologue. */
2607 if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
2609 body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);
2611 if (body_pc == XTENSA_ISA_BADPC)
2615 goto finish_frame_analysis;
2619 /* Get the frame information and FP (if used) at the current PC.
2620 If PC is in the prologue, the prologue analysis is more reliable
2621 than DWARF info. We don't not know for sure, if PC is in the prologue,
2622 but we do know no calls have yet taken place, so we can almost
2623 certainly rely on the prologue analysis. */
2627 /* Prologue analysis was successful up to the PC.
2628 It includes the cases when PC == START_PC. */
2629 c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
2630 /* c0_hasfp == true means there is a frame pointer because
2631 we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
2632 was derived from SP. Otherwise, it would be C0_FP. */
2633 fp_regnum = c0_hasfp ? C0_FP : C0_SP;
2634 c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
2635 fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
2637 else /* No data from the prologue analysis. */
2640 fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
2645 if (cache->c0.c0_fpalign)
2647 /* This frame has a special prologue with a dynamic stack adjustment
2648 to force an alignment, which is bigger than standard 16 bytes. */
2650 CORE_ADDR unaligned_sp;
2652 if (cache->c0.c0_old_sp == C0_INEXP)
2653 /* This can't be. Prologue code should be consistent.
2654 Unaligned stack pointer should be saved in a spare register. */
2658 goto finish_frame_analysis;
2661 if (cache->c0.c0_sp_ofs == C0_NOSTK)
2662 /* Saved unaligned value of SP is kept in a register. */
2663 unaligned_sp = get_frame_register_unsigned
2664 (this_frame, gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_old_sp);
2666 /* Get the value from stack. */
2667 unaligned_sp = (CORE_ADDR)
2668 read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);
2670 prev_sp = unaligned_sp + c0_frmsz;
2673 prev_sp = fp + c0_frmsz;
2675 /* Frame size from debug info or prologue tracking does not account for
2676 alloca() and other dynamic allocations. Adjust frame size by FP - SP. */
2679 fp = get_frame_register_unsigned (this_frame, fp_regnum);
2681 /* Update the stack frame size. */
2682 c0_frmsz += fp - sp;
2685 /* Get the return address (RA) from the stack if saved,
2686 or try to get it from a register. */
2688 to_stk = cache->c0.c0_rt[C0_RA].to_stk;
2689 if (to_stk != C0_NOSTK)
2691 read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
2694 else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
2695 && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
2697 /* Special case for terminating backtrace at a function that wants to
2698 be seen as the outermost one. Such a function will clear it's RA (A0)
2699 register to 0 in the prologue instead of saving its original value. */
2704 /* RA was copied to another register or (before any function call) may
2705 still be in the original RA register. This is not always reliable:
2706 even in a leaf function, register tracking stops after prologue, and
2707 even in prologue, non-prologue instructions (not tracked) may overwrite
2708 RA or any register it was copied to. If likely in prologue or before
2709 any call, use retracking info and hope for the best (compiler should
2710 have saved RA in stack if not in a leaf function). If not in prologue,
2716 && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
2718 if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
2722 ra = get_frame_register_unsigned
2724 gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
2729 finish_frame_analysis:
2730 cache->pc = start_pc;
2732 /* RA == 0 marks the outermost frame. Do not go past it. */
2733 cache->prev_sp = (ra != 0) ? prev_sp : 0;
2734 cache->c0.fp_regnum = fp_regnum;
2735 cache->c0.c0_frmsz = c0_frmsz;
2736 cache->c0.c0_hasfp = c0_hasfp;
2737 cache->c0.c0_fp = fp;
2740 static CORE_ADDR a0_saved;
2741 static CORE_ADDR a7_saved;
2742 static CORE_ADDR a11_saved;
2743 static int a0_was_saved;
2744 static int a7_was_saved;
2745 static int a11_was_saved;
2747 /* Simulate L32E instruction: AT <-- ref (AS + offset). */
2749 execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2751 int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
2752 int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
2753 CORE_ADDR addr = xtensa_read_register (asreg) + offset;
2754 unsigned int spilled_value
2755 = read_memory_unsigned_integer (addr, 4, gdbarch_byte_order (gdbarch));
2757 if ((at == 0) && !a0_was_saved)
2759 a0_saved = xtensa_read_register (atreg);
2762 else if ((at == 7) && !a7_was_saved)
2764 a7_saved = xtensa_read_register (atreg);
2767 else if ((at == 11) && !a11_was_saved)
2769 a11_saved = xtensa_read_register (atreg);
2773 xtensa_write_register (atreg, spilled_value);
2776 /* Simulate S32E instruction: AT --> ref (AS + offset). */
2778 execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2780 int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
2781 int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
2782 CORE_ADDR addr = xtensa_read_register (asreg) + offset;
2783 ULONGEST spilled_value = xtensa_read_register (atreg);
2785 write_memory_unsigned_integer (addr, 4,
2786 gdbarch_byte_order (gdbarch),
2790 #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN 200
2796 xtNoExceptionHandler
2797 } xtensa_exception_handler_t;
2799 /* Execute instruction stream from current PC until hitting RFWU or RFWO.
2800 Return type of Xtensa Window Interrupt Handler on success. */
2801 static xtensa_exception_handler_t
2802 execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
2805 xtensa_insnbuf ins, slot;
2806 gdb_byte ibuf[XTENSA_ISA_BSZ];
2807 CORE_ADDR ia, bt, ba;
2809 int ilen, islots, is;
2813 void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);
2815 uint32_t at, as, offset;
2817 /* WindowUnderflow12 = true, when inside _WindowUnderflow12. */
2818 int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140;
2820 isa = xtensa_default_isa;
2821 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2822 ins = xtensa_insnbuf_alloc (isa);
2823 slot = xtensa_insnbuf_alloc (isa);
2832 while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
2834 if (ia + xtensa_isa_maxlength (isa) > bt)
2837 bt = (ba + XTENSA_ISA_BSZ);
2838 if (target_read_memory (ba, ibuf, bt - ba) != 0)
2839 return xtNoExceptionHandler;
2841 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2842 ifmt = xtensa_format_decode (isa, ins);
2843 if (ifmt == XTENSA_UNDEFINED)
2844 return xtNoExceptionHandler;
2845 ilen = xtensa_format_length (isa, ifmt);
2846 if (ilen == XTENSA_UNDEFINED)
2847 return xtNoExceptionHandler;
2848 islots = xtensa_format_num_slots (isa, ifmt);
2849 if (islots == XTENSA_UNDEFINED)
2850 return xtNoExceptionHandler;
2851 for (is = 0; is < islots; ++is)
2853 if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
2854 return xtNoExceptionHandler;
2855 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2856 if (opc == XTENSA_UNDEFINED)
2857 return xtNoExceptionHandler;
2858 switch (call0_classify_opcode (isa, opc))
2864 /* We expect none of them here. */
2865 return xtNoExceptionHandler;
2867 func = execute_l32e;
2870 func = execute_s32e;
2872 case c0opc_rfwo: /* RFWO. */
2873 /* Here, we return from WindowOverflow handler and,
2874 if we stopped at the very beginning, which means
2875 A0 was saved, we have to restore it now. */
2878 int arreg = arreg_number (gdbarch,
2879 gdbarch_tdep (gdbarch)->a0_base,
2881 xtensa_write_register (arreg, a0_saved);
2883 return xtWindowOverflow;
2884 case c0opc_rfwu: /* RFWU. */
2885 /* Here, we return from WindowUnderflow handler.
2886 Let's see if either A7 or A11 has to be restored. */
2887 if (WindowUnderflow12)
2891 int arreg = arreg_number (gdbarch,
2892 gdbarch_tdep (gdbarch)->a0_base + 11,
2894 xtensa_write_register (arreg, a11_saved);
2897 else if (a7_was_saved)
2899 int arreg = arreg_number (gdbarch,
2900 gdbarch_tdep (gdbarch)->a0_base + 7,
2902 xtensa_write_register (arreg, a7_saved);
2904 return xtWindowUnderflow;
2905 default: /* Simply skip this insns. */
2909 /* Decode arguments for L32E / S32E and simulate their execution. */
2910 if ( xtensa_opcode_num_operands (isa, opc) != 3 )
2911 return xtNoExceptionHandler;
2912 if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, &at))
2913 return xtNoExceptionHandler;
2914 if (xtensa_operand_decode (isa, opc, 0, &at))
2915 return xtNoExceptionHandler;
2916 if (xtensa_operand_get_field (isa, opc, 1, ifmt, is, slot, &as))
2917 return xtNoExceptionHandler;
2918 if (xtensa_operand_decode (isa, opc, 1, &as))
2919 return xtNoExceptionHandler;
2920 if (xtensa_operand_get_field (isa, opc, 2, ifmt, is, slot, &offset))
2921 return xtNoExceptionHandler;
2922 if (xtensa_operand_decode (isa, opc, 2, &offset))
2923 return xtNoExceptionHandler;
2925 (*func) (gdbarch, at, as, offset, wb);
2930 return xtNoExceptionHandler;
2933 /* Handle Window Overflow / Underflow exception frames. */
2936 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
2937 xtensa_frame_cache_t *cache,
2940 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2941 CORE_ADDR ps, wb, ws, ra;
2942 int epc1_regnum, i, regnum;
2943 xtensa_exception_handler_t eh_type;
2945 /* Read PS, WB, and WS from the hardware. Note that PS register
2946 must be present, if Windowed ABI is supported. */
2947 ps = xtensa_read_register (gdbarch_ps_regnum (gdbarch));
2948 wb = xtensa_read_register (gdbarch_tdep (gdbarch)->wb_regnum);
2949 ws = xtensa_read_register (gdbarch_tdep (gdbarch)->ws_regnum);
2951 /* Execute all the remaining instructions from Window Interrupt Handler
2952 by simulating them on the remote protocol level. On return, set the
2953 type of Xtensa Window Interrupt Handler, or report an error. */
2954 eh_type = execute_code (gdbarch, pc, wb);
2955 if (eh_type == xtNoExceptionHandler)
2957 Unable to decode Xtensa Window Interrupt Handler's code."));
2959 cache->ps = ps ^ PS_EXC; /* Clear the exception bit in PS. */
2960 cache->call0 = 0; /* It's Windowed ABI. */
2962 /* All registers for the cached frame will be alive. */
2963 for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
2964 cache->wd.aregs[i] = -1;
2966 if (eh_type == xtWindowOverflow)
2967 cache->wd.ws = ws ^ (1 << wb);
2968 else /* eh_type == xtWindowUnderflow. */
2969 cache->wd.ws = ws | (1 << wb);
2971 cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB. */
2972 regnum = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base,
2974 ra = xtensa_read_register (regnum);
2975 cache->wd.callsize = WINSIZE (ra);
2976 cache->prev_sp = xtensa_read_register (regnum + 1);
2977 /* Set regnum to a frame pointer of the frame being cached. */
2978 regnum = xtensa_scan_prologue (gdbarch, pc);
2979 regnum = arreg_number (gdbarch,
2980 gdbarch_tdep (gdbarch)->a0_base + regnum,
2982 cache->base = get_frame_register_unsigned (this_frame, regnum);
2984 /* Read PC of interrupted function from EPC1 register. */
2985 epc1_regnum = xtensa_find_register_by_name (gdbarch,"epc1");
2986 if (epc1_regnum < 0)
2987 error(_("Unable to read Xtensa register EPC1"));
2988 cache->ra = xtensa_read_register (epc1_regnum);
2989 cache->pc = get_frame_func (this_frame);
2993 /* Skip function prologue.
2995 Return the pc of the first instruction after prologue. GDB calls this to
2996 find the address of the first line of the function or (if there is no line
2997 number information) to skip the prologue for planting breakpoints on
2998 function entries. Use debug info (if present) or prologue analysis to skip
2999 the prologue to achieve reliable debugging behavior. For windowed ABI,
3000 only the 'entry' instruction is skipped. It is not strictly necessary to
3001 skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
3002 backtrace at any point in the prologue, however certain potential hazards
3003 are avoided and a more "normal" debugging experience is ensured by
3004 skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
3005 For example, if we don't skip the prologue:
3006 - Some args may not yet have been saved to the stack where the debug
3007 info expects to find them (true anyway when only 'entry' is skipped);
3008 - Software breakpoints ('break' instrs) may not have been unplanted
3009 when the prologue analysis is done on initializing the frame cache,
3010 and breaks in the prologue will throw off the analysis.
3012 If we have debug info ( line-number info, in particular ) we simply skip
3013 the code associated with the first function line effectively skipping
3014 the prologue code. It works even in cases like
3017 { int local_var = 1;
3021 because, for this source code, both Xtensa compilers will generate two
3022 separate entries ( with the same line number ) in dwarf line-number
3023 section to make sure there is a boundary between the prologue code and
3024 the rest of the function.
3026 If there is no debug info, we need to analyze the code. */
3028 /* #define DONT_SKIP_PROLOGUE */
3031 xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
3033 struct symtab_and_line prologue_sal;
3036 DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
3038 #if DONT_SKIP_PROLOGUE
3042 /* Try to find first body line from debug info. */
3044 prologue_sal = find_pc_line (start_pc, 0);
3045 if (prologue_sal.line != 0) /* Found debug info. */
3047 /* In Call0, it is possible to have a function with only one instruction
3048 ('ret') resulting from a one-line optimized function that does nothing.
3049 In that case, prologue_sal.end may actually point to the start of the
3050 next function in the text section, causing a breakpoint to be set at
3051 the wrong place. Check, if the end address is within a different
3052 function, and if so return the start PC. We know we have symbol
3057 if ((gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
3058 && call0_ret (start_pc, prologue_sal.end))
3061 find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
3062 if (end_func != start_pc)
3065 return prologue_sal.end;
3068 /* No debug line info. Analyze prologue for Call0 or simply skip ENTRY. */
3069 body_pc = call0_analyze_prologue (gdbarch, start_pc, 0, 0,
3070 xtensa_alloc_frame_cache (0));
3071 return body_pc != 0 ? body_pc : start_pc;
3074 /* Verify the current configuration. */
3076 xtensa_verify_config (struct gdbarch *gdbarch)
3078 struct ui_file *log;
3079 struct cleanup *cleanups;
3080 struct gdbarch_tdep *tdep;
3084 tdep = gdbarch_tdep (gdbarch);
3085 log = mem_fileopen ();
3086 cleanups = make_cleanup_ui_file_delete (log);
3088 /* Verify that we got a reasonable number of AREGS. */
3089 if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
3090 fprintf_unfiltered (log, _("\
3091 \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
3094 /* Verify that certain registers exist. */
3096 if (tdep->pc_regnum == -1)
3097 fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
3098 if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
3099 fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));
3101 if (tdep->isa_use_windowed_registers)
3103 if (tdep->wb_regnum == -1)
3104 fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
3105 if (tdep->ws_regnum == -1)
3106 fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
3107 if (tdep->ar_base == -1)
3108 fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
3111 if (tdep->a0_base == -1)
3112 fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
3114 buf = ui_file_xstrdup (log, &length);
3115 make_cleanup (xfree, buf);
3117 internal_error (__FILE__, __LINE__,
3118 _("the following are invalid: %s"), buf);
3119 do_cleanups (cleanups);
3123 /* Derive specific register numbers from the array of registers. */
3126 xtensa_derive_tdep (struct gdbarch_tdep *tdep)
3128 xtensa_register_t* rmap;
3129 int n, max_size = 4;
3132 tdep->num_nopriv_regs = 0;
3134 /* Special registers 0..255 (core). */
3135 #define XTENSA_DBREGN_SREG(n) (0x0200+(n))
3137 for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
3139 if (rmap->target_number == 0x0020)
3140 tdep->pc_regnum = n;
3141 else if (rmap->target_number == 0x0100)
3143 else if (rmap->target_number == 0x0000)
3145 else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
3146 tdep->wb_regnum = n;
3147 else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
3148 tdep->ws_regnum = n;
3149 else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
3150 tdep->debugcause_regnum = n;
3151 else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
3152 tdep->exccause_regnum = n;
3153 else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
3154 tdep->excvaddr_regnum = n;
3155 else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
3156 tdep->lbeg_regnum = n;
3157 else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
3158 tdep->lend_regnum = n;
3159 else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
3160 tdep->lcount_regnum = n;
3161 else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
3162 tdep->sar_regnum = n;
3163 else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
3164 tdep->litbase_regnum = n;
3165 else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
3166 tdep->ps_regnum = n;
3168 else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
3169 tdep->interrupt_regnum = n;
3170 else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
3171 tdep->interrupt2_regnum = n;
3172 else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
3173 tdep->cpenable_regnum = n;
3176 if (rmap->byte_size > max_size)
3177 max_size = rmap->byte_size;
3178 if (rmap->mask != 0 && tdep->num_regs == 0)
3180 /* Find out out how to deal with priveleged registers.
3182 if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3183 && tdep->num_nopriv_regs == 0)
3184 tdep->num_nopriv_regs = n;
3186 if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3187 && tdep->num_regs == 0)
3191 /* Number of pseudo registers. */
3192 tdep->num_pseudo_regs = n - tdep->num_regs;
3194 /* Empirically determined maximum sizes. */
3195 tdep->max_register_raw_size = max_size;
3196 tdep->max_register_virtual_size = max_size;
3199 /* Module "constructor" function. */
3201 extern struct gdbarch_tdep xtensa_tdep;
3203 static struct gdbarch *
3204 xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3206 struct gdbarch_tdep *tdep;
3207 struct gdbarch *gdbarch;
3208 struct xtensa_abi_handler *abi_handler;
3210 DEBUGTRACE ("gdbarch_init()\n");
3212 /* We have to set the byte order before we call gdbarch_alloc. */
3213 info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
3215 tdep = &xtensa_tdep;
3216 gdbarch = gdbarch_alloc (&info, tdep);
3217 xtensa_derive_tdep (tdep);
3219 /* Verify our configuration. */
3220 xtensa_verify_config (gdbarch);
3221 xtensa_session_once_reported = 0;
3223 /* Pseudo-Register read/write. */
3224 set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
3225 set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
3227 /* Set target information. */
3228 set_gdbarch_num_regs (gdbarch, tdep->num_regs);
3229 set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
3230 set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
3231 set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
3232 set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
3234 /* Renumber registers for known formats (stabs and dwarf2). */
3235 set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
3236 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
3238 /* We provide our own function to get register information. */
3239 set_gdbarch_register_name (gdbarch, xtensa_register_name);
3240 set_gdbarch_register_type (gdbarch, xtensa_register_type);
3242 /* To call functions from GDB using dummy frame. */
3243 set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
3245 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
3247 set_gdbarch_return_value (gdbarch, xtensa_return_value);
3249 /* Advance PC across any prologue instructions to reach "real" code. */
3250 set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
3252 /* Stack grows downward. */
3253 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3255 /* Set breakpoints. */
3256 set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
3258 /* After breakpoint instruction or illegal instruction, pc still
3259 points at break instruction, so don't decrement. */
3260 set_gdbarch_decr_pc_after_break (gdbarch, 0);
3262 /* We don't skip args. */
3263 set_gdbarch_frame_args_skip (gdbarch, 0);
3265 set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
3267 set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
3269 set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
3271 /* Frame handling. */
3272 frame_base_set_default (gdbarch, &xtensa_frame_base);
3273 frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
3274 dwarf2_append_unwinders (gdbarch);
3276 set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
3278 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
3280 xtensa_add_reggroups (gdbarch);
3281 set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
3283 set_gdbarch_regset_from_core_section (gdbarch,
3284 xtensa_regset_from_core_section);
3286 set_solib_svr4_fetch_link_map_offsets
3287 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
3293 xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3295 error (_("xtensa_dump_tdep(): not implemented"));
3298 /* Provide a prototype to silence -Wmissing-prototypes. */
3299 extern initialize_file_ftype _initialize_xtensa_tdep;
3302 _initialize_xtensa_tdep (void)
3304 struct cmd_list_element *c;
3306 gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
3307 xtensa_init_reggroups ();
3309 add_setshow_zuinteger_cmd ("xtensa",
3311 &xtensa_debug_level,
3312 _("Set Xtensa debugging."),
3313 _("Show Xtensa debugging."), _("\
3314 When non-zero, Xtensa-specific debugging is enabled. \
3315 Can be 1, 2, 3, or 4 indicating the level of debugging."),
3318 &setdebuglist, &showdebuglist);