1 /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
3 Copyright (C) 2003, 2005-2012 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"
41 #include "frame-base.h"
42 #include "frame-unwind.h"
44 #include "arch-utils.h"
51 #include "gdb_assert.h"
53 #include "xtensa-isa.h"
54 #include "xtensa-tdep.h"
55 #include "xtensa-config.h"
58 static int xtensa_debug_level = 0;
60 #define DEBUGWARN(args...) \
61 if (xtensa_debug_level > 0) \
62 fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
64 #define DEBUGINFO(args...) \
65 if (xtensa_debug_level > 1) \
66 fprintf_unfiltered (gdb_stdlog, "(info ) " args)
68 #define DEBUGTRACE(args...) \
69 if (xtensa_debug_level > 2) \
70 fprintf_unfiltered (gdb_stdlog, "(trace) " args)
72 #define DEBUGVERB(args...) \
73 if (xtensa_debug_level > 3) \
74 fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
77 /* According to the ABI, the SP must be aligned to 16-byte boundaries. */
78 #define SP_ALIGNMENT 16
81 /* On Windowed ABI, we use a6 through a11 for passing arguments
82 to a function called by GDB because CALL4 is used. */
83 #define ARGS_NUM_REGS 6
84 #define REGISTER_SIZE 4
87 /* Extract the call size from the return address or PS register. */
88 #define PS_CALLINC_SHIFT 16
89 #define PS_CALLINC_MASK 0x00030000
90 #define CALLINC(ps) (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
91 #define WINSIZE(ra) (4 * (( (ra) >> 30) & 0x3))
93 /* On TX, hardware can be configured without Exception Option.
94 There is no PS register in this case. Inside XT-GDB, let us treat
95 it as a virtual read-only register always holding the same value. */
98 /* ABI-independent macros. */
99 #define ARG_NOF(gdbarch) \
100 (gdbarch_tdep (gdbarch)->call_abi \
101 == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
102 #define ARG_1ST(gdbarch) \
103 (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only \
104 ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
105 : (gdbarch_tdep (gdbarch)->a0_base + 6))
107 /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
108 indicates that the instruction is an ENTRY instruction. */
110 #define XTENSA_IS_ENTRY(gdbarch, op1) \
111 ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
112 ? ((op1) == 0x6c) : ((op1) == 0x36))
114 #define XTENSA_ENTRY_LENGTH 3
116 /* windowing_enabled() returns true, if windowing is enabled.
117 WOE must be set to 1; EXCM to 0.
118 Note: We assume that EXCM is always 0 for XEA1. */
120 #define PS_WOE (1<<18)
121 #define PS_EXC (1<<4)
124 windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
126 /* If we know CALL0 ABI is set explicitly, say it is Call0. */
127 if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
130 return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
133 /* Convert a live A-register number to the corresponding AR-register
136 arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
138 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
141 arreg = a_regnum - tdep->a0_base;
142 arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
143 arreg &= tdep->num_aregs - 1;
145 return arreg + tdep->ar_base;
148 /* Convert a live AR-register number to the corresponding A-register order
149 number in a range [0..15]. Return -1, if AR_REGNUM is out of WB window. */
151 areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
153 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
156 areg = ar_regnum - tdep->ar_base;
157 if (areg < 0 || areg >= tdep->num_aregs)
159 areg = (areg - wb * 4) & (tdep->num_aregs - 1);
160 return (areg > 15) ? -1 : areg;
163 /* Read Xtensa register directly from the hardware. */
165 xtensa_read_register (int regnum)
169 regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
170 return (unsigned long) value;
173 /* Write Xtensa register directly to the hardware. */
175 xtensa_write_register (int regnum, ULONGEST value)
177 regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
180 /* Return the window size of the previous call to the function from which we
183 This function is used to extract the return value after a called function
184 has returned to the caller. On Xtensa, the register that holds the return
185 value (from the perspective of the caller) depends on what call
186 instruction was used. For now, we are assuming that the call instruction
187 precedes the current address, so we simply analyze the call instruction.
188 If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
189 method to call the inferior function. */
192 extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
194 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
199 DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
201 /* Read the previous instruction (should be a call[x]{4|8|12}. */
202 read_memory (pc-3, buf, 3);
203 insn = extract_unsigned_integer (buf, 3, byte_order);
205 /* Decode call instruction:
207 call{0,4,8,12} OFFSET || {00,01,10,11} || 0101
208 callx{0,4,8,12} OFFSET || 11 || {00,01,10,11} || 0000
210 call{0,4,8,12} 0101 || {00,01,10,11} || OFFSET
211 callx{0,4,8,12} 0000 || {00,01,10,11} || 11 || OFFSET. */
213 if (byte_order == BFD_ENDIAN_LITTLE)
215 if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
216 winsize = (insn & 0x30) >> 2; /* 0, 4, 8, 12. */
220 if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
221 winsize = (insn >> 16) & 0xc; /* 0, 4, 8, 12. */
227 /* REGISTER INFORMATION */
229 /* Find register by name. */
231 xtensa_find_register_by_name (struct gdbarch *gdbarch, char *name)
235 for (i = 0; i < gdbarch_num_regs (gdbarch)
236 + gdbarch_num_pseudo_regs (gdbarch);
239 if (strcasecmp (gdbarch_tdep (gdbarch)->regmap[i].name, name) == 0)
245 /* Returns the name of a register. */
247 xtensa_register_name (struct gdbarch *gdbarch, int regnum)
249 /* Return the name stored in the register map. */
250 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
251 + gdbarch_num_pseudo_regs (gdbarch))
252 return gdbarch_tdep (gdbarch)->regmap[regnum].name;
254 internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
258 /* Return the type of a register. Create a new type, if necessary. */
261 xtensa_register_type (struct gdbarch *gdbarch, int regnum)
263 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
265 /* Return signed integer for ARx and Ax registers. */
266 if ((regnum >= tdep->ar_base
267 && regnum < tdep->ar_base + tdep->num_aregs)
268 || (regnum >= tdep->a0_base
269 && regnum < tdep->a0_base + 16))
270 return builtin_type (gdbarch)->builtin_int;
272 if (regnum == gdbarch_pc_regnum (gdbarch)
273 || regnum == tdep->a0_base + 1)
274 return builtin_type (gdbarch)->builtin_data_ptr;
276 /* Return the stored type for all other registers. */
277 else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
278 + gdbarch_num_pseudo_regs (gdbarch))
280 xtensa_register_t* reg = &tdep->regmap[regnum];
282 /* Set ctype for this register (only the first time). */
286 struct ctype_cache *tp;
287 int size = reg->byte_size;
289 /* We always use the memory representation,
290 even if the register width is smaller. */
294 reg->ctype = builtin_type (gdbarch)->builtin_uint8;
298 reg->ctype = builtin_type (gdbarch)->builtin_uint16;
302 reg->ctype = builtin_type (gdbarch)->builtin_uint32;
306 reg->ctype = builtin_type (gdbarch)->builtin_uint64;
310 reg->ctype = builtin_type (gdbarch)->builtin_uint128;
314 for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
315 if (tp->size == size)
320 char *name = xstrprintf ("int%d", size * 8);
321 tp = xmalloc (sizeof (struct ctype_cache));
322 tp->next = tdep->type_entries;
323 tdep->type_entries = tp;
326 = arch_integer_type (gdbarch, size * 8, 1, name);
330 reg->ctype = tp->virtual_type;
336 internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
341 /* Return the 'local' register number for stubs, dwarf2, etc.
342 The debugging information enumerates registers starting from 0 for A0
343 to n for An. So, we only have to add the base number for A0. */
346 xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
350 if (regnum >= 0 && regnum < 16)
351 return gdbarch_tdep (gdbarch)->a0_base + regnum;
354 i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
356 if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
359 internal_error (__FILE__, __LINE__,
360 _("invalid dwarf/stabs register number %d"), regnum);
365 /* Write the bits of a masked register to the various registers.
366 Only the masked areas of these registers are modified; the other
367 fields are untouched. The size of masked registers is always less
368 than or equal to 32 bits. */
371 xtensa_register_write_masked (struct regcache *regcache,
372 xtensa_register_t *reg, const gdb_byte *buffer)
374 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
375 const xtensa_mask_t *mask = reg->mask;
377 int shift = 0; /* Shift for next mask (mod 32). */
378 int start, size; /* Start bit and size of current mask. */
380 unsigned int *ptr = value;
381 unsigned int regval, m, mem = 0;
383 int bytesize = reg->byte_size;
384 int bitsize = bytesize * 8;
387 DEBUGTRACE ("xtensa_register_write_masked ()\n");
389 /* Copy the masked register to host byte-order. */
390 if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
391 for (i = 0; i < bytesize; i++)
394 mem |= (buffer[bytesize - i - 1] << 24);
399 for (i = 0; i < bytesize; i++)
402 mem |= (buffer[i] << 24);
407 /* We might have to shift the final value:
408 bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
409 bytesize & 3 == x -> shift (4-x) * 8. */
411 *ptr = mem >> (((0 - bytesize) & 3) * 8);
415 /* Write the bits to the masked areas of the other registers. */
416 for (i = 0; i < mask->count; i++)
418 start = mask->mask[i].bit_start;
419 size = mask->mask[i].bit_size;
420 regval = mem >> shift;
422 if ((shift += size) > bitsize)
423 error (_("size of all masks is larger than the register"));
432 regval |= mem << (size - shift);
435 /* Make sure we have a valid register. */
436 r = mask->mask[i].reg_num;
437 if (r >= 0 && size > 0)
439 /* Don't overwrite the unmasked areas. */
441 regcache_cooked_read_unsigned (regcache, r, &old_val);
442 m = 0xffffffff >> (32 - size) << start;
444 regval = (regval & m) | (old_val & ~m);
445 regcache_cooked_write_unsigned (regcache, r, regval);
451 /* Read a tie state or mapped registers. Read the masked areas
452 of the registers and assemble them into a single value. */
454 static enum register_status
455 xtensa_register_read_masked (struct regcache *regcache,
456 xtensa_register_t *reg, gdb_byte *buffer)
458 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
459 const xtensa_mask_t *mask = reg->mask;
464 unsigned int *ptr = value;
465 unsigned int regval, mem = 0;
467 int bytesize = reg->byte_size;
468 int bitsize = bytesize * 8;
471 DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
472 reg->name == 0 ? "" : reg->name);
474 /* Assemble the register from the masked areas of other registers. */
475 for (i = 0; i < mask->count; i++)
477 int r = mask->mask[i].reg_num;
480 enum register_status status;
483 status = regcache_cooked_read_unsigned (regcache, r, &val);
484 if (status != REG_VALID)
486 regval = (unsigned int) val;
491 start = mask->mask[i].bit_start;
492 size = mask->mask[i].bit_size;
497 regval &= (0xffffffff >> (32 - size));
499 mem |= regval << shift;
501 if ((shift += size) > bitsize)
502 error (_("size of all masks is larger than the register"));
513 mem = regval >> (size - shift);
520 /* Copy value to target byte order. */
524 if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
525 for (i = 0; i < bytesize; i++)
529 buffer[bytesize - i - 1] = mem & 0xff;
533 for (i = 0; i < bytesize; i++)
537 buffer[i] = mem & 0xff;
545 /* Read pseudo registers. */
547 static enum register_status
548 xtensa_pseudo_register_read (struct gdbarch *gdbarch,
549 struct regcache *regcache,
553 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
555 DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
556 regnum, xtensa_register_name (gdbarch, regnum));
558 if (regnum == gdbarch_num_regs (gdbarch)
559 + gdbarch_num_pseudo_regs (gdbarch) - 1)
560 regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
562 /* Read aliases a0..a15, if this is a Windowed ABI. */
563 if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
564 && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
565 && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
567 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
568 enum register_status status;
570 status = regcache_raw_read (regcache,
571 gdbarch_tdep (gdbarch)->wb_regnum,
573 if (status != REG_VALID)
575 regnum = arreg_number (gdbarch, regnum,
576 extract_unsigned_integer (buf, 4, byte_order));
579 /* We can always read non-pseudo registers. */
580 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
581 return regcache_raw_read (regcache, regnum, buffer);
583 /* We have to find out how to deal with priveleged registers.
584 Let's treat them as pseudo-registers, but we cannot read/write them. */
586 else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
588 buffer[0] = (gdb_byte)0;
589 buffer[1] = (gdb_byte)0;
590 buffer[2] = (gdb_byte)0;
591 buffer[3] = (gdb_byte)0;
594 /* Pseudo registers. */
596 && regnum < gdbarch_num_regs (gdbarch)
597 + gdbarch_num_pseudo_regs (gdbarch))
599 xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
600 xtensa_register_type_t type = reg->type;
601 int flags = gdbarch_tdep (gdbarch)->target_flags;
603 /* We cannot read Unknown or Unmapped registers. */
604 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
606 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
608 warning (_("cannot read register %s"),
609 xtensa_register_name (gdbarch, regnum));
614 /* Some targets cannot read TIE register files. */
615 else if (type == xtRegisterTypeTieRegfile)
617 /* Use 'fetch' to get register? */
618 if (flags & xtTargetFlagsUseFetchStore)
620 warning (_("cannot read register"));
624 /* On some targets (esp. simulators), we can always read the reg. */
625 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
627 warning (_("cannot read register"));
632 /* We can always read mapped registers. */
633 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
634 return xtensa_register_read_masked (regcache, reg, buffer);
636 /* Assume that we can read the register. */
637 return regcache_raw_read (regcache, regnum, buffer);
640 internal_error (__FILE__, __LINE__,
641 _("invalid register number %d"), regnum);
645 /* Write pseudo registers. */
648 xtensa_pseudo_register_write (struct gdbarch *gdbarch,
649 struct regcache *regcache,
651 const gdb_byte *buffer)
653 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
655 DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
656 regnum, xtensa_register_name (gdbarch, regnum));
658 if (regnum == gdbarch_num_regs (gdbarch)
659 + gdbarch_num_pseudo_regs (gdbarch) -1)
660 regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
662 /* Renumber register, if aliase a0..a15 on Windowed ABI. */
663 if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
664 && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
665 && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
667 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
669 regcache_raw_read (regcache,
670 gdbarch_tdep (gdbarch)->wb_regnum, buf);
671 regnum = arreg_number (gdbarch, regnum,
672 extract_unsigned_integer (buf, 4, byte_order));
675 /* We can always write 'core' registers.
676 Note: We might have converted Ax->ARy. */
677 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
678 regcache_raw_write (regcache, regnum, buffer);
680 /* We have to find out how to deal with priveleged registers.
681 Let's treat them as pseudo-registers, but we cannot read/write them. */
683 else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
687 /* Pseudo registers. */
689 && regnum < gdbarch_num_regs (gdbarch)
690 + gdbarch_num_pseudo_regs (gdbarch))
692 xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
693 xtensa_register_type_t type = reg->type;
694 int flags = gdbarch_tdep (gdbarch)->target_flags;
696 /* On most targets, we cannot write registers
697 of type "Unknown" or "Unmapped". */
698 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
700 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
702 warning (_("cannot write register %s"),
703 xtensa_register_name (gdbarch, regnum));
708 /* Some targets cannot read TIE register files. */
709 else if (type == xtRegisterTypeTieRegfile)
711 /* Use 'store' to get register? */
712 if (flags & xtTargetFlagsUseFetchStore)
714 warning (_("cannot write register"));
718 /* On some targets (esp. simulators), we can always write
720 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
722 warning (_("cannot write register"));
727 /* We can always write mapped registers. */
728 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
730 xtensa_register_write_masked (regcache, reg, buffer);
734 /* Assume that we can write the register. */
735 regcache_raw_write (regcache, regnum, buffer);
738 internal_error (__FILE__, __LINE__,
739 _("invalid register number %d"), regnum);
742 static struct reggroup *xtensa_ar_reggroup;
743 static struct reggroup *xtensa_user_reggroup;
744 static struct reggroup *xtensa_vectra_reggroup;
745 static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
748 xtensa_init_reggroups (void)
751 char cpname[] = "cp0";
753 xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
754 xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
755 xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
757 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
760 xtensa_cp[i] = reggroup_new (cpname, USER_REGGROUP);
765 xtensa_add_reggroups (struct gdbarch *gdbarch)
769 /* Predefined groups. */
770 reggroup_add (gdbarch, all_reggroup);
771 reggroup_add (gdbarch, save_reggroup);
772 reggroup_add (gdbarch, restore_reggroup);
773 reggroup_add (gdbarch, system_reggroup);
774 reggroup_add (gdbarch, vector_reggroup);
775 reggroup_add (gdbarch, general_reggroup);
776 reggroup_add (gdbarch, float_reggroup);
778 /* Xtensa-specific groups. */
779 reggroup_add (gdbarch, xtensa_ar_reggroup);
780 reggroup_add (gdbarch, xtensa_user_reggroup);
781 reggroup_add (gdbarch, xtensa_vectra_reggroup);
783 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
784 reggroup_add (gdbarch, xtensa_cp[i]);
788 xtensa_coprocessor_register_group (struct reggroup *group)
792 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
793 if (group == xtensa_cp[i])
799 #define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
800 | XTENSA_REGISTER_FLAGS_WRITABLE \
801 | XTENSA_REGISTER_FLAGS_VOLATILE)
803 #define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
804 | XTENSA_REGISTER_FLAGS_WRITABLE)
807 xtensa_register_reggroup_p (struct gdbarch *gdbarch,
809 struct reggroup *group)
811 xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
812 xtensa_register_type_t type = reg->type;
813 xtensa_register_group_t rg = reg->group;
816 if (group == save_reggroup)
817 /* Every single register should be included into the list of registers
818 to be watched for changes while using -data-list-changed-registers. */
821 /* First, skip registers that are not visible to this target
822 (unknown and unmapped registers when not using ISS). */
824 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
826 if (group == all_reggroup)
828 if (group == xtensa_ar_reggroup)
829 return rg & xtRegisterGroupAddrReg;
830 if (group == xtensa_user_reggroup)
831 return rg & xtRegisterGroupUser;
832 if (group == float_reggroup)
833 return rg & xtRegisterGroupFloat;
834 if (group == general_reggroup)
835 return rg & xtRegisterGroupGeneral;
836 if (group == system_reggroup)
837 return rg & xtRegisterGroupState;
838 if (group == vector_reggroup || group == xtensa_vectra_reggroup)
839 return rg & xtRegisterGroupVectra;
840 if (group == restore_reggroup)
841 return (regnum < gdbarch_num_regs (gdbarch)
842 && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
843 cp_number = xtensa_coprocessor_register_group (group);
845 return rg & (xtRegisterGroupCP0 << cp_number);
851 /* Supply register REGNUM from the buffer specified by GREGS and LEN
852 in the general-purpose register set REGSET to register cache
853 REGCACHE. If REGNUM is -1 do this for all registers in REGSET. */
856 xtensa_supply_gregset (const struct regset *regset,
862 const xtensa_elf_gregset_t *regs = gregs;
863 struct gdbarch *gdbarch = get_regcache_arch (rc);
866 DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);
868 if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
869 regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) ®s->pc);
870 if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
871 regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) ®s->ps);
872 if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
873 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
874 (char *) ®s->windowbase);
875 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
876 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
877 (char *) ®s->windowstart);
878 if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
879 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
880 (char *) ®s->lbeg);
881 if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
882 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
883 (char *) ®s->lend);
884 if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
885 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
886 (char *) ®s->lcount);
887 if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
888 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
889 (char *) ®s->sar);
890 if (regnum >=gdbarch_tdep (gdbarch)->ar_base
891 && regnum < gdbarch_tdep (gdbarch)->ar_base
892 + gdbarch_tdep (gdbarch)->num_aregs)
893 regcache_raw_supply (rc, regnum,
894 (char *) ®s->ar[regnum - gdbarch_tdep
895 (gdbarch)->ar_base]);
896 else if (regnum == -1)
898 for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
899 regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
900 (char *) ®s->ar[i]);
905 /* Xtensa register set. */
911 xtensa_supply_gregset
915 /* Return the appropriate register set for the core
916 section identified by SECT_NAME and SECT_SIZE. */
918 static const struct regset *
919 xtensa_regset_from_core_section (struct gdbarch *core_arch,
920 const char *sect_name,
923 DEBUGTRACE ("xtensa_regset_from_core_section "
924 "(..., sect_name==\"%s\", sect_size==%x)\n",
925 sect_name, (unsigned int) sect_size);
927 if (strcmp (sect_name, ".reg") == 0
928 && sect_size >= sizeof(xtensa_elf_gregset_t))
929 return &xtensa_gregset;
935 /* Handling frames. */
937 /* Number of registers to save in case of Windowed ABI. */
938 #define XTENSA_NUM_SAVED_AREGS 12
940 /* Frame cache part for Windowed ABI. */
941 typedef struct xtensa_windowed_frame_cache
943 int wb; /* WINDOWBASE of the previous frame. */
944 int callsize; /* Call size of this frame. */
945 int ws; /* WINDOWSTART of the previous frame. It keeps track of
946 life windows only. If there is no bit set for the
947 window, that means it had been already spilled
948 because of window overflow. */
950 /* Addresses of spilled A-registers.
951 AREGS[i] == -1, if corresponding AR is alive. */
952 CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
953 } xtensa_windowed_frame_cache_t;
955 /* Call0 ABI Definitions. */
957 #define C0_MAXOPDS 3 /* Maximum number of operands for prologue
959 #define C0_NREGS 16 /* Number of A-registers to track. */
960 #define C0_CLESV 12 /* Callee-saved registers are here and up. */
961 #define C0_SP 1 /* Register used as SP. */
962 #define C0_FP 15 /* Register used as FP. */
963 #define C0_RA 0 /* Register used as return address. */
964 #define C0_ARGS 2 /* Register used as first arg/retval. */
965 #define C0_NARGS 6 /* Number of A-regs for args/retvals. */
967 /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
968 A-register where the current content of the reg came from (in terms
969 of an original reg and a constant). Negative values of c0_rt[n].fp_reg
970 mean that the orignal content of the register was saved to the stack.
971 c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
972 know where SP will end up until the entire prologue has been analyzed. */
974 #define C0_CONST -1 /* fr_reg value if register contains a constant. */
975 #define C0_INEXP -2 /* fr_reg value if inexpressible as reg + offset. */
976 #define C0_NOSTK -1 /* to_stk value if register has not been stored. */
978 extern xtensa_isa xtensa_default_isa;
980 typedef struct xtensa_c0reg
982 int fr_reg; /* original register from which register content
983 is derived, or C0_CONST, or C0_INEXP. */
984 int fr_ofs; /* constant offset from reg, or immediate value. */
985 int to_stk; /* offset from original SP to register (4-byte aligned),
986 or C0_NOSTK if register has not been saved. */
989 /* Frame cache part for Call0 ABI. */
990 typedef struct xtensa_call0_frame_cache
992 int c0_frmsz; /* Stack frame size. */
993 int c0_hasfp; /* Current frame uses frame pointer. */
994 int fp_regnum; /* A-register used as FP. */
995 int c0_fp; /* Actual value of frame pointer. */
996 int c0_fpalign; /* Dinamic adjustment for the stack
997 pointer. It's an AND mask. Zero,
998 if alignment was not adjusted. */
999 int c0_old_sp; /* In case of dynamic adjustment, it is
1000 a register holding unaligned sp.
1001 C0_INEXP, when undefined. */
1002 int c0_sp_ofs; /* If "c0_old_sp" was spilled it's a
1003 stack offset. C0_NOSTK otherwise. */
1005 xtensa_c0reg_t c0_rt[C0_NREGS]; /* Register tracking information. */
1006 } xtensa_call0_frame_cache_t;
1008 typedef struct xtensa_frame_cache
1010 CORE_ADDR base; /* Stack pointer of this frame. */
1011 CORE_ADDR pc; /* PC of this frame at the function entry point. */
1012 CORE_ADDR ra; /* The raw return address of this frame. */
1013 CORE_ADDR ps; /* The PS register of the previous (older) frame. */
1014 CORE_ADDR prev_sp; /* Stack Pointer of the previous (older) frame. */
1015 int call0; /* It's a call0 framework (else windowed). */
1018 xtensa_windowed_frame_cache_t wd; /* call0 == false. */
1019 xtensa_call0_frame_cache_t c0; /* call0 == true. */
1021 } xtensa_frame_cache_t;
1024 static struct xtensa_frame_cache *
1025 xtensa_alloc_frame_cache (int windowed)
1027 xtensa_frame_cache_t *cache;
1030 DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
1032 cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
1039 cache->call0 = !windowed;
1042 cache->c0.c0_frmsz = -1;
1043 cache->c0.c0_hasfp = 0;
1044 cache->c0.fp_regnum = -1;
1045 cache->c0.c0_fp = -1;
1046 cache->c0.c0_fpalign = 0;
1047 cache->c0.c0_old_sp = C0_INEXP;
1048 cache->c0.c0_sp_ofs = C0_NOSTK;
1050 for (i = 0; i < C0_NREGS; i++)
1052 cache->c0.c0_rt[i].fr_reg = i;
1053 cache->c0.c0_rt[i].fr_ofs = 0;
1054 cache->c0.c0_rt[i].to_stk = C0_NOSTK;
1061 cache->wd.callsize = -1;
1063 for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
1064 cache->wd.aregs[i] = -1;
1071 xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
1073 return address & ~15;
1078 xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1083 DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n",
1084 host_address_to_string (next_frame));
1086 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1087 pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1089 DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
1095 static struct frame_id
1096 xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1100 /* THIS-FRAME is a dummy frame. Return a frame ID of that frame. */
1102 pc = get_frame_pc (this_frame);
1103 fp = get_frame_register_unsigned
1104 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1106 /* Make dummy frame ID unique by adding a constant. */
1107 return frame_id_build (fp + SP_ALIGNMENT, pc);
1110 /* Returns true, if instruction to execute next is unique to Xtensa Window
1111 Interrupt Handlers. It can only be one of L32E, S32E, RFWO, or RFWU. */
1114 xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
1116 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1117 unsigned int insn = read_memory_integer (pc, 4, byte_order);
1120 if (byte_order == BFD_ENDIAN_BIG)
1122 /* Check, if this is L32E or S32E. */
1123 code = insn & 0xf000ff00;
1124 if ((code == 0x00009000) || (code == 0x00009400))
1126 /* Check, if this is RFWU or RFWO. */
1127 code = insn & 0xffffff00;
1128 return ((code == 0x00430000) || (code == 0x00530000));
1132 /* Check, if this is L32E or S32E. */
1133 code = insn & 0x00ff000f;
1134 if ((code == 0x090000) || (code == 0x490000))
1136 /* Check, if this is RFWU or RFWO. */
1137 code = insn & 0x00ffffff;
1138 return ((code == 0x00003400) || (code == 0x00003500));
1142 /* Returns the best guess about which register is a frame pointer
1143 for the function containing CURRENT_PC. */
1145 #define XTENSA_ISA_BSZ 32 /* Instruction buffer size. */
1146 #define XTENSA_ISA_BADPC ((CORE_ADDR)0) /* Bad PC value. */
1149 xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
1151 #define RETURN_FP goto done
1153 unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
1154 CORE_ADDR start_addr;
1156 xtensa_insnbuf ins, slot;
1157 char ibuf[XTENSA_ISA_BSZ];
1158 CORE_ADDR ia, bt, ba;
1160 int ilen, islots, is;
1162 const char *opcname;
1164 find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
1165 if (start_addr == 0)
1168 if (!xtensa_default_isa)
1169 xtensa_default_isa = xtensa_isa_init (0, 0);
1170 isa = xtensa_default_isa;
1171 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
1172 ins = xtensa_insnbuf_alloc (isa);
1173 slot = xtensa_insnbuf_alloc (isa);
1176 for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
1178 if (ia + xtensa_isa_maxlength (isa) > bt)
1181 bt = (ba + XTENSA_ISA_BSZ) < current_pc
1182 ? ba + XTENSA_ISA_BSZ : current_pc;
1183 if (target_read_memory (ba, ibuf, bt - ba) != 0)
1187 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
1188 ifmt = xtensa_format_decode (isa, ins);
1189 if (ifmt == XTENSA_UNDEFINED)
1191 ilen = xtensa_format_length (isa, ifmt);
1192 if (ilen == XTENSA_UNDEFINED)
1194 islots = xtensa_format_num_slots (isa, ifmt);
1195 if (islots == XTENSA_UNDEFINED)
1198 for (is = 0; is < islots; ++is)
1200 if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
1203 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
1204 if (opc == XTENSA_UNDEFINED)
1207 opcname = xtensa_opcode_name (isa, opc);
1209 if (strcasecmp (opcname, "mov.n") == 0
1210 || strcasecmp (opcname, "or") == 0)
1212 unsigned int register_operand;
1214 /* Possible candidate for setting frame pointer
1215 from A1. This is what we are looking for. */
1217 if (xtensa_operand_get_field (isa, opc, 1, ifmt,
1218 is, slot, ®ister_operand) != 0)
1220 if (xtensa_operand_decode (isa, opc, 1, ®ister_operand) != 0)
1222 if (register_operand == 1) /* Mov{.n} FP A1. */
1224 if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
1225 ®ister_operand) != 0)
1227 if (xtensa_operand_decode (isa, opc, 0,
1228 ®ister_operand) != 0)
1232 = gdbarch_tdep (gdbarch)->a0_base + register_operand;
1238 /* We have problems decoding the memory. */
1240 || strcasecmp (opcname, "ill") == 0
1241 || strcasecmp (opcname, "ill.n") == 0
1242 /* Hit planted breakpoint. */
1243 || strcasecmp (opcname, "break") == 0
1244 || strcasecmp (opcname, "break.n") == 0
1245 /* Flow control instructions finish prologue. */
1246 || xtensa_opcode_is_branch (isa, opc) > 0
1247 || xtensa_opcode_is_jump (isa, opc) > 0
1248 || xtensa_opcode_is_loop (isa, opc) > 0
1249 || xtensa_opcode_is_call (isa, opc) > 0
1250 || strcasecmp (opcname, "simcall") == 0
1251 || strcasecmp (opcname, "syscall") == 0)
1252 /* Can not continue analysis. */
1257 xtensa_insnbuf_free(isa, slot);
1258 xtensa_insnbuf_free(isa, ins);
1262 /* The key values to identify the frame using "cache" are
1264 cache->base = SP (or best guess about FP) of this frame;
1265 cache->pc = entry-PC (entry point of the frame function);
1266 cache->prev_sp = SP of the previous frame. */
1269 call0_frame_cache (struct frame_info *this_frame,
1270 xtensa_frame_cache_t *cache, CORE_ADDR pc);
1273 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
1274 xtensa_frame_cache_t *cache,
1277 static struct xtensa_frame_cache *
1278 xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
1280 xtensa_frame_cache_t *cache;
1281 CORE_ADDR ra, wb, ws, pc, sp, ps;
1282 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1283 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1284 unsigned int fp_regnum;
1285 int windowed, ps_regnum;
1290 pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
1291 ps_regnum = gdbarch_ps_regnum (gdbarch);
1292 ps = (ps_regnum >= 0
1293 ? get_frame_register_unsigned (this_frame, ps_regnum) : TX_PS);
1295 windowed = windowing_enabled (gdbarch, ps);
1297 /* Get pristine xtensa-frame. */
1298 cache = xtensa_alloc_frame_cache (windowed);
1299 *this_cache = cache;
1305 /* Get WINDOWBASE, WINDOWSTART, and PS registers. */
1306 wb = get_frame_register_unsigned (this_frame,
1307 gdbarch_tdep (gdbarch)->wb_regnum);
1308 ws = get_frame_register_unsigned (this_frame,
1309 gdbarch_tdep (gdbarch)->ws_regnum);
1311 op1 = read_memory_integer (pc, 1, byte_order);
1312 if (XTENSA_IS_ENTRY (gdbarch, op1))
1314 int callinc = CALLINC (ps);
1315 ra = get_frame_register_unsigned
1316 (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
1318 /* ENTRY hasn't been executed yet, therefore callsize is still 0. */
1319 cache->wd.callsize = 0;
1322 cache->prev_sp = get_frame_register_unsigned
1323 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1325 /* This only can be the outermost frame since we are
1326 just about to execute ENTRY. SP hasn't been set yet.
1327 We can assume any frame size, because it does not
1328 matter, and, let's fake frame base in cache. */
1329 cache->base = cache->prev_sp - 16;
1332 cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1333 cache->ps = (ps & ~PS_CALLINC_MASK)
1334 | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1340 fp_regnum = xtensa_scan_prologue (gdbarch, pc);
1341 ra = get_frame_register_unsigned (this_frame,
1342 gdbarch_tdep (gdbarch)->a0_base);
1343 cache->wd.callsize = WINSIZE (ra);
1344 cache->wd.wb = (wb - cache->wd.callsize / 4)
1345 & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
1346 cache->wd.ws = ws & ~(1 << wb);
1348 cache->pc = get_frame_func (this_frame);
1349 cache->ra = (pc & 0xc0000000) | (ra & 0x3fffffff);
1350 cache->ps = (ps & ~PS_CALLINC_MASK)
1351 | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1354 if (cache->wd.ws == 0)
1359 sp = get_frame_register_unsigned
1360 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
1362 for (i = 0; i < 4; i++, sp += 4)
1364 cache->wd.aregs[i] = sp;
1367 if (cache->wd.callsize > 4)
1369 /* Set A4...A7/A11. */
1370 /* Get the SP of the frame previous to the previous one.
1371 To achieve this, we have to dereference SP twice. */
1372 sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
1373 sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
1374 sp -= cache->wd.callsize * 4;
1376 for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
1378 cache->wd.aregs[i] = sp;
1383 if ((cache->prev_sp == 0) && ( ra != 0 ))
1384 /* If RA is equal to 0 this frame is an outermost frame. Leave
1385 cache->prev_sp unchanged marking the boundary of the frame stack. */
1387 if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
1389 /* Register window overflow already happened.
1390 We can read caller's SP from the proper spill loction. */
1391 sp = get_frame_register_unsigned
1392 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1393 cache->prev_sp = read_memory_integer (sp - 12, 4, byte_order);
1397 /* Read caller's frame SP directly from the previous window. */
1398 int regnum = arreg_number
1399 (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
1402 cache->prev_sp = xtensa_read_register (regnum);
1406 else if (xtensa_window_interrupt_insn (gdbarch, pc))
1408 /* Execution stopped inside Xtensa Window Interrupt Handler. */
1410 xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
1411 /* Everything was set already, including cache->base. */
1414 else /* Call0 framework. */
1416 call0_frame_cache (this_frame, cache, pc);
1417 fp_regnum = cache->c0.fp_regnum;
1420 cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
1425 static int xtensa_session_once_reported = 1;
1427 /* Report a problem with prologue analysis while doing backtracing.
1428 But, do it only once to avoid annoyng repeated messages. */
1433 if (xtensa_session_once_reported == 0)
1435 \nUnrecognised function prologue. Stack trace cannot be resolved. \
1436 This message will not be repeated in this session.\n"));
1438 xtensa_session_once_reported = 1;
1443 xtensa_frame_this_id (struct frame_info *this_frame,
1445 struct frame_id *this_id)
1447 struct xtensa_frame_cache *cache =
1448 xtensa_frame_cache (this_frame, this_cache);
1450 if (cache->prev_sp == 0)
1453 (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
1456 static struct value *
1457 xtensa_frame_prev_register (struct frame_info *this_frame,
1461 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1462 struct xtensa_frame_cache *cache;
1463 ULONGEST saved_reg = 0;
1466 if (*this_cache == NULL)
1467 *this_cache = xtensa_frame_cache (this_frame, this_cache);
1468 cache = *this_cache;
1470 if (regnum ==gdbarch_pc_regnum (gdbarch))
1471 saved_reg = cache->ra;
1472 else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
1473 saved_reg = cache->prev_sp;
1474 else if (!cache->call0)
1476 if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
1477 saved_reg = cache->wd.ws;
1478 else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
1479 saved_reg = cache->wd.wb;
1480 else if (regnum == gdbarch_ps_regnum (gdbarch))
1481 saved_reg = cache->ps;
1489 return frame_unwind_got_constant (this_frame, regnum, saved_reg);
1491 if (!cache->call0) /* Windowed ABI. */
1493 /* Convert A-register numbers to AR-register numbers,
1494 if we deal with A-register. */
1495 if (regnum >= gdbarch_tdep (gdbarch)->a0_base
1496 && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
1497 regnum = arreg_number (gdbarch, regnum, cache->wd.wb);
1499 /* Check, if we deal with AR-register saved on stack. */
1500 if (regnum >= gdbarch_tdep (gdbarch)->ar_base
1501 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1502 + gdbarch_tdep (gdbarch)->num_aregs))
1504 int areg = areg_number (gdbarch, regnum, cache->wd.wb);
1507 && areg < XTENSA_NUM_SAVED_AREGS
1508 && cache->wd.aregs[areg] != -1)
1509 return frame_unwind_got_memory (this_frame, regnum,
1510 cache->wd.aregs[areg]);
1513 else /* Call0 ABI. */
1515 int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
1516 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1518 ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
1525 /* If register was saved in the prologue, retrieve it. */
1526 stkofs = cache->c0.c0_rt[reg].to_stk;
1527 if (stkofs != C0_NOSTK)
1529 /* Determine SP on entry based on FP. */
1530 spe = cache->c0.c0_fp
1531 - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1533 return frame_unwind_got_memory (this_frame, regnum,
1539 /* All other registers have been either saved to
1540 the stack or are still alive in the processor. */
1542 return frame_unwind_got_register (this_frame, regnum, regnum);
1546 static const struct frame_unwind
1550 default_frame_unwind_stop_reason,
1551 xtensa_frame_this_id,
1552 xtensa_frame_prev_register,
1554 default_frame_sniffer
1558 xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
1560 struct xtensa_frame_cache *cache =
1561 xtensa_frame_cache (this_frame, this_cache);
1566 static const struct frame_base
1570 xtensa_frame_base_address,
1571 xtensa_frame_base_address,
1572 xtensa_frame_base_address
1577 xtensa_extract_return_value (struct type *type,
1578 struct regcache *regcache,
1581 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1582 bfd_byte *valbuf = dst;
1583 int len = TYPE_LENGTH (type);
1588 DEBUGTRACE ("xtensa_extract_return_value (...)\n");
1590 gdb_assert(len > 0);
1592 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1594 /* First, we have to find the caller window in the register file. */
1595 regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1596 callsize = extract_call_winsize (gdbarch, pc);
1598 /* On Xtensa, we can return up to 4 words (or 2 for call12). */
1599 if (len > (callsize > 8 ? 8 : 16))
1600 internal_error (__FILE__, __LINE__,
1601 _("cannot extract return value of %d bytes long"),
1604 /* Get the register offset of the return
1605 register (A2) in the caller window. */
1606 regcache_raw_read_unsigned
1607 (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1608 areg = arreg_number (gdbarch,
1609 gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1613 /* No windowing hardware - Call0 ABI. */
1614 areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1617 DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
1619 if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1622 for (; len > 0; len -= 4, areg++, valbuf += 4)
1625 regcache_raw_read_part (regcache, areg, offset, len, valbuf);
1627 regcache_raw_read (regcache, areg, valbuf);
1633 xtensa_store_return_value (struct type *type,
1634 struct regcache *regcache,
1637 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1638 const bfd_byte *valbuf = dst;
1642 int len = TYPE_LENGTH (type);
1645 DEBUGTRACE ("xtensa_store_return_value (...)\n");
1647 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1649 regcache_raw_read_unsigned
1650 (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1651 regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1652 callsize = extract_call_winsize (gdbarch, pc);
1654 if (len > (callsize > 8 ? 8 : 16))
1655 internal_error (__FILE__, __LINE__,
1656 _("unimplemented for this length: %d"),
1657 TYPE_LENGTH (type));
1658 areg = arreg_number (gdbarch,
1659 gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1661 DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
1662 callsize, (int) wb);
1666 areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1669 if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1672 for (; len > 0; len -= 4, areg++, valbuf += 4)
1675 regcache_raw_write_part (regcache, areg, offset, len, valbuf);
1677 regcache_raw_write (regcache, areg, valbuf);
1682 static enum return_value_convention
1683 xtensa_return_value (struct gdbarch *gdbarch,
1684 struct value *function,
1685 struct type *valtype,
1686 struct regcache *regcache,
1688 const gdb_byte *writebuf)
1690 /* Structures up to 16 bytes are returned in registers. */
1692 int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1693 || TYPE_CODE (valtype) == TYPE_CODE_UNION
1694 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1695 && TYPE_LENGTH (valtype) > 16);
1698 return RETURN_VALUE_STRUCT_CONVENTION;
1700 DEBUGTRACE ("xtensa_return_value(...)\n");
1702 if (writebuf != NULL)
1704 xtensa_store_return_value (valtype, regcache, writebuf);
1707 if (readbuf != NULL)
1709 gdb_assert (!struct_return);
1710 xtensa_extract_return_value (valtype, regcache, readbuf);
1712 return RETURN_VALUE_REGISTER_CONVENTION;
1719 xtensa_push_dummy_call (struct gdbarch *gdbarch,
1720 struct value *function,
1721 struct regcache *regcache,
1724 struct value **args,
1727 CORE_ADDR struct_addr)
1729 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1731 int size, onstack_size;
1732 gdb_byte *buf = (gdb_byte *) alloca (16);
1734 struct argument_info
1736 const bfd_byte *contents;
1738 int onstack; /* onstack == 0 => in reg */
1739 int align; /* alignment */
1742 int offset; /* stack offset if on stack. */
1743 int regno; /* regno if in register. */
1747 struct argument_info *arg_info =
1748 (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
1752 DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
1754 if (xtensa_debug_level > 3)
1757 DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
1758 DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
1759 "struct_addr=0x%x\n",
1760 (int) sp, (int) struct_return, (int) struct_addr);
1762 for (i = 0; i < nargs; i++)
1764 struct value *arg = args[i];
1765 struct type *arg_type = check_typedef (value_type (arg));
1766 fprintf_unfiltered (gdb_stdlog, "%2d: %s %3d ", i,
1767 host_address_to_string (arg),
1768 TYPE_LENGTH (arg_type));
1769 switch (TYPE_CODE (arg_type))
1772 fprintf_unfiltered (gdb_stdlog, "int");
1774 case TYPE_CODE_STRUCT:
1775 fprintf_unfiltered (gdb_stdlog, "struct");
1778 fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
1781 fprintf_unfiltered (gdb_stdlog, " %s\n",
1782 host_address_to_string (value_contents (arg)));
1786 /* First loop: collect information.
1787 Cast into type_long. (This shouldn't happen often for C because
1788 GDB already does this earlier.) It's possible that GDB could
1789 do it all the time but it's harmless to leave this code here. */
1796 size = REGISTER_SIZE;
1798 for (i = 0; i < nargs; i++)
1800 struct argument_info *info = &arg_info[i];
1801 struct value *arg = args[i];
1802 struct type *arg_type = check_typedef (value_type (arg));
1804 switch (TYPE_CODE (arg_type))
1807 case TYPE_CODE_BOOL:
1808 case TYPE_CODE_CHAR:
1809 case TYPE_CODE_RANGE:
1810 case TYPE_CODE_ENUM:
1812 /* Cast argument to long if necessary as the mask does it too. */
1813 if (TYPE_LENGTH (arg_type)
1814 < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
1816 arg_type = builtin_type (gdbarch)->builtin_long;
1817 arg = value_cast (arg_type, arg);
1819 /* Aligment is equal to the type length for the basic types. */
1820 info->align = TYPE_LENGTH (arg_type);
1825 /* Align doubles correctly. */
1826 if (TYPE_LENGTH (arg_type)
1827 == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
1828 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
1830 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1833 case TYPE_CODE_STRUCT:
1835 info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1838 info->length = TYPE_LENGTH (arg_type);
1839 info->contents = value_contents (arg);
1841 /* Align size and onstack_size. */
1842 size = (size + info->align - 1) & ~(info->align - 1);
1843 onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
1845 if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
1848 info->u.offset = onstack_size;
1849 onstack_size += info->length;
1854 info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
1856 size += info->length;
1859 /* Adjust the stack pointer and align it. */
1860 sp = align_down (sp - onstack_size, SP_ALIGNMENT);
1862 /* Simulate MOVSP, if Windowed ABI. */
1863 if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1866 read_memory (osp - 16, buf, 16);
1867 write_memory (sp - 16, buf, 16);
1870 /* Second Loop: Load arguments. */
1874 store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
1875 regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
1878 for (i = 0; i < nargs; i++)
1880 struct argument_info *info = &arg_info[i];
1884 int n = info->length;
1885 CORE_ADDR offset = sp + info->u.offset;
1887 /* Odd-sized structs are aligned to the lower side of a memory
1888 word in big-endian mode and require a shift. This only
1889 applies for structures smaller than one word. */
1891 if (n < REGISTER_SIZE
1892 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1893 offset += (REGISTER_SIZE - n);
1895 write_memory (offset, info->contents, info->length);
1900 int n = info->length;
1901 const bfd_byte *cp = info->contents;
1902 int r = info->u.regno;
1904 /* Odd-sized structs are aligned to the lower side of registers in
1905 big-endian mode and require a shift. The odd-sized leftover will
1906 be at the end. Note that this is only true for structures smaller
1907 than REGISTER_SIZE; for larger odd-sized structures the excess
1908 will be left-aligned in the register on both endiannesses. */
1910 if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
1913 v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
1914 v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
1916 store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
1917 regcache_cooked_write (regcache, r, buf);
1919 cp += REGISTER_SIZE;
1926 regcache_cooked_write (regcache, r, cp);
1928 cp += REGISTER_SIZE;
1935 /* Set the return address of dummy frame to the dummy address.
1936 The return address for the current function (in A0) is
1937 saved in the dummy frame, so we can savely overwrite A0 here. */
1939 if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1943 ra = (bp_addr & 0x3fffffff) | 0x40000000;
1944 regcache_raw_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), &val);
1945 ps = (unsigned long) val & ~0x00030000;
1946 regcache_cooked_write_unsigned
1947 (regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
1948 regcache_cooked_write_unsigned (regcache,
1949 gdbarch_ps_regnum (gdbarch),
1952 /* All the registers have been saved. After executing
1953 dummy call, they all will be restored. So it's safe
1954 to modify WINDOWSTART register to make it look like there
1955 is only one register window corresponding to WINDOWEBASE. */
1957 regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
1958 regcache_cooked_write_unsigned
1959 (regcache, gdbarch_tdep (gdbarch)->ws_regnum,
1960 1 << extract_unsigned_integer (buf, 4, byte_order));
1964 /* Simulate CALL0: write RA into A0 register. */
1965 regcache_cooked_write_unsigned
1966 (regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
1969 /* Set new stack pointer and return it. */
1970 regcache_cooked_write_unsigned (regcache,
1971 gdbarch_tdep (gdbarch)->a0_base + 1, sp);
1972 /* Make dummy frame ID unique by adding a constant. */
1973 return sp + SP_ALIGNMENT;
1977 /* Return a breakpoint for the current location of PC. We always use
1978 the density version if we have density instructions (regardless of the
1979 current instruction at PC), and use regular instructions otherwise. */
1981 #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
1982 #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
1983 #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
1984 #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
1986 static const unsigned char *
1987 xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1990 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
1991 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
1992 static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
1993 static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;
1995 DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
1997 if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
1999 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2001 *lenptr = sizeof (density_big_breakpoint);
2002 return density_big_breakpoint;
2006 *lenptr = sizeof (density_little_breakpoint);
2007 return density_little_breakpoint;
2012 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2014 *lenptr = sizeof (big_breakpoint);
2015 return big_breakpoint;
2019 *lenptr = sizeof (little_breakpoint);
2020 return little_breakpoint;
2025 /* Call0 ABI support routines. */
2027 /* Return true, if PC points to "ret" or "ret.n". */
2030 call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
2032 #define RETURN_RET goto done
2034 xtensa_insnbuf ins, slot;
2035 char ibuf[XTENSA_ISA_BSZ];
2036 CORE_ADDR ia, bt, ba;
2038 int ilen, islots, is;
2040 const char *opcname;
2043 isa = xtensa_default_isa;
2044 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2045 ins = xtensa_insnbuf_alloc (isa);
2046 slot = xtensa_insnbuf_alloc (isa);
2049 for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
2051 if (ia + xtensa_isa_maxlength (isa) > bt)
2054 bt = (ba + XTENSA_ISA_BSZ) < finish_pc
2055 ? ba + XTENSA_ISA_BSZ : finish_pc;
2056 if (target_read_memory (ba, ibuf, bt - ba) != 0 )
2060 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2061 ifmt = xtensa_format_decode (isa, ins);
2062 if (ifmt == XTENSA_UNDEFINED)
2064 ilen = xtensa_format_length (isa, ifmt);
2065 if (ilen == XTENSA_UNDEFINED)
2067 islots = xtensa_format_num_slots (isa, ifmt);
2068 if (islots == XTENSA_UNDEFINED)
2071 for (is = 0; is < islots; ++is)
2073 if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
2076 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2077 if (opc == XTENSA_UNDEFINED)
2080 opcname = xtensa_opcode_name (isa, opc);
2082 if ((strcasecmp (opcname, "ret.n") == 0)
2083 || (strcasecmp (opcname, "ret") == 0))
2091 xtensa_insnbuf_free(isa, slot);
2092 xtensa_insnbuf_free(isa, ins);
2096 /* Call0 opcode class. Opcodes are preclassified according to what they
2097 mean for Call0 prologue analysis, and their number of significant operands.
2098 The purpose of this is to simplify prologue analysis by separating
2099 instruction decoding (libisa) from the semantics of prologue analysis. */
2103 c0opc_illegal, /* Unknown to libisa (invalid) or 'ill' opcode. */
2104 c0opc_uninteresting, /* Not interesting for Call0 prologue analysis. */
2105 c0opc_flow, /* Flow control insn. */
2106 c0opc_entry, /* ENTRY indicates non-Call0 prologue. */
2107 c0opc_break, /* Debugger software breakpoints. */
2108 c0opc_add, /* Adding two registers. */
2109 c0opc_addi, /* Adding a register and an immediate. */
2110 c0opc_and, /* Bitwise "and"-ing two registers. */
2111 c0opc_sub, /* Subtracting a register from a register. */
2112 c0opc_mov, /* Moving a register to a register. */
2113 c0opc_movi, /* Moving an immediate to a register. */
2114 c0opc_l32r, /* Loading a literal. */
2115 c0opc_s32i, /* Storing word at fixed offset from a base register. */
2116 c0opc_rwxsr, /* RSR, WRS, or XSR instructions. */
2117 c0opc_l32e, /* L32E instruction. */
2118 c0opc_s32e, /* S32E instruction. */
2119 c0opc_rfwo, /* RFWO instruction. */
2120 c0opc_rfwu, /* RFWU instruction. */
2121 c0opc_NrOf /* Number of opcode classifications. */
2124 /* Return true, if OPCNAME is RSR, WRS, or XSR instruction. */
2127 rwx_special_register (const char *opcname)
2129 char ch = *opcname++;
2131 if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
2133 if (*opcname++ != 's')
2135 if (*opcname++ != 'r')
2137 if (*opcname++ != '.')
2143 /* Classify an opcode based on what it means for Call0 prologue analysis. */
2145 static xtensa_insn_kind
2146 call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
2148 const char *opcname;
2149 xtensa_insn_kind opclass = c0opc_uninteresting;
2151 DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
2153 /* Get opcode name and handle special classifications. */
2155 opcname = xtensa_opcode_name (isa, opc);
2158 || strcasecmp (opcname, "ill") == 0
2159 || strcasecmp (opcname, "ill.n") == 0)
2160 opclass = c0opc_illegal;
2161 else if (strcasecmp (opcname, "break") == 0
2162 || strcasecmp (opcname, "break.n") == 0)
2163 opclass = c0opc_break;
2164 else if (strcasecmp (opcname, "entry") == 0)
2165 opclass = c0opc_entry;
2166 else if (strcasecmp (opcname, "rfwo") == 0)
2167 opclass = c0opc_rfwo;
2168 else if (strcasecmp (opcname, "rfwu") == 0)
2169 opclass = c0opc_rfwu;
2170 else if (xtensa_opcode_is_branch (isa, opc) > 0
2171 || xtensa_opcode_is_jump (isa, opc) > 0
2172 || xtensa_opcode_is_loop (isa, opc) > 0
2173 || xtensa_opcode_is_call (isa, opc) > 0
2174 || strcasecmp (opcname, "simcall") == 0
2175 || strcasecmp (opcname, "syscall") == 0)
2176 opclass = c0opc_flow;
2178 /* Also, classify specific opcodes that need to be tracked. */
2179 else if (strcasecmp (opcname, "add") == 0
2180 || strcasecmp (opcname, "add.n") == 0)
2181 opclass = c0opc_add;
2182 else if (strcasecmp (opcname, "and") == 0)
2183 opclass = c0opc_and;
2184 else if (strcasecmp (opcname, "addi") == 0
2185 || strcasecmp (opcname, "addi.n") == 0
2186 || strcasecmp (opcname, "addmi") == 0)
2187 opclass = c0opc_addi;
2188 else if (strcasecmp (opcname, "sub") == 0)
2189 opclass = c0opc_sub;
2190 else if (strcasecmp (opcname, "mov.n") == 0
2191 || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro. */
2192 opclass = c0opc_mov;
2193 else if (strcasecmp (opcname, "movi") == 0
2194 || strcasecmp (opcname, "movi.n") == 0)
2195 opclass = c0opc_movi;
2196 else if (strcasecmp (opcname, "l32r") == 0)
2197 opclass = c0opc_l32r;
2198 else if (strcasecmp (opcname, "s32i") == 0
2199 || strcasecmp (opcname, "s32i.n") == 0)
2200 opclass = c0opc_s32i;
2201 else if (strcasecmp (opcname, "l32e") == 0)
2202 opclass = c0opc_l32e;
2203 else if (strcasecmp (opcname, "s32e") == 0)
2204 opclass = c0opc_s32e;
2205 else if (rwx_special_register (opcname))
2206 opclass = c0opc_rwxsr;
2211 /* Tracks register movement/mutation for a given operation, which may
2212 be within a bundle. Updates the destination register tracking info
2213 accordingly. The pc is needed only for pc-relative load instructions
2214 (eg. l32r). The SP register number is needed to identify stores to
2215 the stack frame. Returns 0, if analysis was succesfull, non-zero
2219 call0_track_op (struct gdbarch *gdbarch, xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
2220 xtensa_insn_kind opclass, int nods, unsigned odv[],
2221 CORE_ADDR pc, int spreg, xtensa_frame_cache_t *cache)
2223 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2224 unsigned litbase, litaddr, litval;
2229 /* 3 operands: dst, src, imm. */
2230 gdb_assert (nods == 3);
2231 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2232 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
2235 /* 3 operands: dst, src1, src2. */
2236 gdb_assert (nods == 3);
2237 if (src[odv[1]].fr_reg == C0_CONST)
2239 dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
2240 dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
2242 else if (src[odv[2]].fr_reg == C0_CONST)
2244 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2245 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
2247 else dst[odv[0]].fr_reg = C0_INEXP;
2250 /* 3 operands: dst, src1, src2. */
2251 gdb_assert (nods == 3);
2252 if (cache->c0.c0_fpalign == 0)
2254 /* Handle dynamic stack alignment. */
2255 if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
2257 if (src[odv[2]].fr_reg == C0_CONST)
2258 cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
2261 else if ((src[odv[0]].fr_reg == spreg)
2262 && (src[odv[2]].fr_reg == spreg))
2264 if (src[odv[1]].fr_reg == C0_CONST)
2265 cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
2268 /* else fall through. */
2270 if (src[odv[1]].fr_reg == C0_CONST)
2272 dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
2273 dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs & src[odv[1]].fr_ofs;
2275 else if (src[odv[2]].fr_reg == C0_CONST)
2277 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2278 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs & src[odv[2]].fr_ofs;
2280 else dst[odv[0]].fr_reg = C0_INEXP;
2283 /* 3 operands: dst, src1, src2. */
2284 gdb_assert (nods == 3);
2285 if (src[odv[2]].fr_reg == C0_CONST)
2287 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2288 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
2290 else dst[odv[0]].fr_reg = C0_INEXP;
2293 /* 2 operands: dst, src [, src]. */
2294 gdb_assert (nods == 2);
2295 /* First, check if it's a special case of saving unaligned SP
2296 to a spare register in case of dynamic stack adjustment.
2297 But, only do it one time. The second time could be initializing
2298 frame pointer. We don't want to overwrite the first one. */
2299 if ((odv[1] == spreg) && (cache->c0.c0_old_sp == C0_INEXP))
2300 cache->c0.c0_old_sp = odv[0];
2302 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2303 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
2306 /* 2 operands: dst, imm. */
2307 gdb_assert (nods == 2);
2308 dst[odv[0]].fr_reg = C0_CONST;
2309 dst[odv[0]].fr_ofs = odv[1];
2312 /* 2 operands: dst, literal offset. */
2313 gdb_assert (nods == 2);
2314 /* litbase = xtensa_get_litbase (pc); can be also used. */
2315 litbase = (gdbarch_tdep (gdbarch)->litbase_regnum == -1)
2316 ? 0 : xtensa_read_register
2317 (gdbarch_tdep (gdbarch)->litbase_regnum);
2318 litaddr = litbase & 1
2319 ? (litbase & ~1) + (signed)odv[1]
2320 : (pc + 3 + (signed)odv[1]) & ~3;
2321 litval = read_memory_integer (litaddr, 4, byte_order);
2322 dst[odv[0]].fr_reg = C0_CONST;
2323 dst[odv[0]].fr_ofs = litval;
2326 /* 3 operands: value, base, offset. */
2327 gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
2328 /* First, check if it's a spill for saved unaligned SP,
2329 when dynamic stack adjustment was applied to this frame. */
2330 if ((cache->c0.c0_fpalign != 0) /* Dynamic stack adjustment. */
2331 && (odv[1] == spreg) /* SP usage indicates spill. */
2332 && (odv[0] == cache->c0.c0_old_sp)) /* Old SP register spilled. */
2333 cache->c0.c0_sp_ofs = odv[2];
2335 if (src[odv[1]].fr_reg == spreg /* Store to stack frame. */
2336 && (src[odv[1]].fr_ofs & 3) == 0 /* Alignment preserved. */
2337 && src[odv[0]].fr_reg >= 0 /* Value is from a register. */
2338 && src[odv[0]].fr_ofs == 0 /* Value hasn't been modified. */
2339 && src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time. */
2341 /* ISA encoding guarantees alignment. But, check it anyway. */
2342 gdb_assert ((odv[2] & 3) == 0);
2343 dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
2346 /* If we end up inside Window Overflow / Underflow interrupt handler
2347 report an error because these handlers should have been handled
2348 already in a different way. */
2360 /* Analyze prologue of the function at start address to determine if it uses
2361 the Call0 ABI, and if so track register moves and linear modifications
2362 in the prologue up to the PC or just beyond the prologue, whichever is
2363 first. An 'entry' instruction indicates non-Call0 ABI and the end of the
2364 prologue. The prologue may overlap non-prologue instructions but is
2365 guaranteed to end by the first flow-control instruction (jump, branch,
2366 call or return). Since an optimized function may move information around
2367 and change the stack frame arbitrarily during the prologue, the information
2368 is guaranteed valid only at the point in the function indicated by the PC.
2369 May be used to skip the prologue or identify the ABI, w/o tracking.
2371 Returns: Address of first instruction after prologue, or PC (whichever
2372 is first), or 0, if decoding failed (in libisa).
2374 start Start address of function/prologue.
2375 pc Program counter to stop at. Use 0 to continue to end of prologue.
2376 If 0, avoids infinite run-on in corrupt code memory by bounding
2377 the scan to the end of the function if that can be determined.
2378 nregs Number of general registers to track.
2380 cache Xtensa frame cache.
2382 Note that these may produce useful results even if decoding fails
2383 because they begin with default assumptions that analysis may change. */
2386 call0_analyze_prologue (struct gdbarch *gdbarch,
2387 CORE_ADDR start, CORE_ADDR pc,
2388 int nregs, xtensa_frame_cache_t *cache)
2390 CORE_ADDR ia; /* Current insn address in prologue. */
2391 CORE_ADDR ba = 0; /* Current address at base of insn buffer. */
2392 CORE_ADDR bt; /* Current address at top+1 of insn buffer. */
2393 char ibuf[XTENSA_ISA_BSZ];/* Instruction buffer for decoding prologue. */
2394 xtensa_isa isa; /* libisa ISA handle. */
2395 xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot. */
2396 xtensa_format ifmt; /* libisa instruction format. */
2397 int ilen, islots, is; /* Instruction length, nbr slots, current slot. */
2398 xtensa_opcode opc; /* Opcode in current slot. */
2399 xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis. */
2400 int nods; /* Opcode number of operands. */
2401 unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa. */
2402 xtensa_c0reg_t *rtmp; /* Register tracking info snapshot. */
2403 int j; /* General loop counter. */
2404 int fail = 0; /* Set non-zero and exit, if decoding fails. */
2405 CORE_ADDR body_pc; /* The PC for the first non-prologue insn. */
2406 CORE_ADDR end_pc; /* The PC for the lust function insn. */
2408 struct symtab_and_line prologue_sal;
2410 DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
2411 (int)start, (int)pc);
2413 /* Try to limit the scan to the end of the function if a non-zero pc
2414 arg was not supplied to avoid probing beyond the end of valid memory.
2415 If memory is full of garbage that classifies as c0opc_uninteresting.
2416 If this fails (eg. if no symbols) pc ends up 0 as it was.
2417 Intialize the Call0 frame and register tracking info.
2418 Assume it's Call0 until an 'entry' instruction is encountered.
2419 Assume we may be in the prologue until we hit a flow control instr. */
2425 /* Find out, if we have an information about the prologue from DWARF. */
2426 prologue_sal = find_pc_line (start, 0);
2427 if (prologue_sal.line != 0) /* Found debug info. */
2428 body_pc = prologue_sal.end;
2430 /* If we are going to analyze the prologue in general without knowing about
2431 the current PC, make the best assumtion for the end of the prologue. */
2434 find_pc_partial_function (start, 0, NULL, &end_pc);
2435 body_pc = min (end_pc, body_pc);
2438 body_pc = min (pc, body_pc);
2441 rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
2443 if (!xtensa_default_isa)
2444 xtensa_default_isa = xtensa_isa_init (0, 0);
2445 isa = xtensa_default_isa;
2446 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2447 ins = xtensa_insnbuf_alloc (isa);
2448 slot = xtensa_insnbuf_alloc (isa);
2450 for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
2452 /* (Re)fill instruction buffer from memory if necessary, but do not
2453 read memory beyond PC to be sure we stay within text section
2454 (this protection only works if a non-zero pc is supplied). */
2456 if (ia + xtensa_isa_maxlength (isa) > bt)
2459 bt = (ba + XTENSA_ISA_BSZ) < body_pc ? ba + XTENSA_ISA_BSZ : body_pc;
2460 if (target_read_memory (ba, ibuf, bt - ba) != 0 )
2461 error (_("Unable to read target memory ..."));
2464 /* Decode format information. */
2466 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2467 ifmt = xtensa_format_decode (isa, ins);
2468 if (ifmt == XTENSA_UNDEFINED)
2473 ilen = xtensa_format_length (isa, ifmt);
2474 if (ilen == XTENSA_UNDEFINED)
2479 islots = xtensa_format_num_slots (isa, ifmt);
2480 if (islots == XTENSA_UNDEFINED)
2486 /* Analyze a bundle or a single instruction, using a snapshot of
2487 the register tracking info as input for the entire bundle so that
2488 register changes do not take effect within this bundle. */
2490 for (j = 0; j < nregs; ++j)
2491 rtmp[j] = cache->c0.c0_rt[j];
2493 for (is = 0; is < islots; ++is)
2495 /* Decode a slot and classify the opcode. */
2497 fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
2501 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2502 DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
2504 if (opc == XTENSA_UNDEFINED)
2505 opclass = c0opc_illegal;
2507 opclass = call0_classify_opcode (isa, opc);
2509 /* Decide whether to track this opcode, ignore it, or bail out. */
2518 case c0opc_uninteresting:
2521 case c0opc_flow: /* Flow control instructions stop analysis. */
2522 case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis. */
2527 ia += ilen; /* Skip over 'entry' insn. */
2534 /* Only expected opcodes should get this far. */
2536 /* Extract and decode the operands. */
2537 nods = xtensa_opcode_num_operands (isa, opc);
2538 if (nods == XTENSA_UNDEFINED)
2544 for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
2546 fail = xtensa_operand_get_field (isa, opc, j, ifmt,
2551 fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
2556 /* Check operands to verify use of 'mov' assembler macro. */
2557 if (opclass == c0opc_mov && nods == 3)
2559 if (odv[2] == odv[1])
2562 if ((odv[0] == 1) && (odv[1] != 1))
2563 /* OR A1, An, An , where n != 1.
2564 This means we are inside epilogue already. */
2569 opclass = c0opc_uninteresting;
2574 /* Track register movement and modification for this operation. */
2575 fail = call0_track_op (gdbarch, cache->c0.c0_rt, rtmp,
2576 opclass, nods, odv, ia, 1, cache);
2582 DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
2583 (unsigned)ia, fail ? "failed" : "succeeded");
2584 xtensa_insnbuf_free(isa, slot);
2585 xtensa_insnbuf_free(isa, ins);
2586 return fail ? XTENSA_ISA_BADPC : ia;
2589 /* Initialize frame cache for the current frame in CALL0 ABI. */
2592 call0_frame_cache (struct frame_info *this_frame,
2593 xtensa_frame_cache_t *cache, CORE_ADDR pc)
2595 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2596 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2597 CORE_ADDR start_pc; /* The beginning of the function. */
2598 CORE_ADDR body_pc=UINT_MAX; /* PC, where prologue analysis stopped. */
2599 CORE_ADDR sp, fp, ra;
2600 int fp_regnum = C0_SP, c0_hasfp = 0, c0_frmsz = 0, prev_sp = 0, to_stk;
2602 sp = get_frame_register_unsigned
2603 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
2604 fp = sp; /* Assume FP == SP until proven otherwise. */
2606 /* Find the beginning of the prologue of the function containing the PC
2607 and analyze it up to the PC or the end of the prologue. */
2609 if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
2611 body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);
2613 if (body_pc == XTENSA_ISA_BADPC)
2617 goto finish_frame_analysis;
2621 /* Get the frame information and FP (if used) at the current PC.
2622 If PC is in the prologue, the prologue analysis is more reliable
2623 than DWARF info. We don't not know for sure, if PC is in the prologue,
2624 but we do know no calls have yet taken place, so we can almost
2625 certainly rely on the prologue analysis. */
2629 /* Prologue analysis was successful up to the PC.
2630 It includes the cases when PC == START_PC. */
2631 c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
2632 /* c0_hasfp == true means there is a frame pointer because
2633 we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
2634 was derived from SP. Otherwise, it would be C0_FP. */
2635 fp_regnum = c0_hasfp ? C0_FP : C0_SP;
2636 c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
2637 fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
2639 else /* No data from the prologue analysis. */
2642 fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
2647 if (cache->c0.c0_fpalign)
2649 /* This frame has a special prologue with a dynamic stack adjustment
2650 to force an alignment, which is bigger than standard 16 bytes. */
2652 CORE_ADDR unaligned_sp;
2654 if (cache->c0.c0_old_sp == C0_INEXP)
2655 /* This can't be. Prologue code should be consistent.
2656 Unaligned stack pointer should be saved in a spare register. */
2660 goto finish_frame_analysis;
2663 if (cache->c0.c0_sp_ofs == C0_NOSTK)
2664 /* Saved unaligned value of SP is kept in a register. */
2665 unaligned_sp = get_frame_register_unsigned
2666 (this_frame, gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_old_sp);
2668 /* Get the value from stack. */
2669 unaligned_sp = (CORE_ADDR)
2670 read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);
2672 prev_sp = unaligned_sp + c0_frmsz;
2675 prev_sp = fp + c0_frmsz;
2677 /* Frame size from debug info or prologue tracking does not account for
2678 alloca() and other dynamic allocations. Adjust frame size by FP - SP. */
2681 fp = get_frame_register_unsigned (this_frame, fp_regnum);
2683 /* Update the stack frame size. */
2684 c0_frmsz += fp - sp;
2687 /* Get the return address (RA) from the stack if saved,
2688 or try to get it from a register. */
2690 to_stk = cache->c0.c0_rt[C0_RA].to_stk;
2691 if (to_stk != C0_NOSTK)
2693 read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
2696 else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
2697 && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
2699 /* Special case for terminating backtrace at a function that wants to
2700 be seen as the outermost one. Such a function will clear it's RA (A0)
2701 register to 0 in the prologue instead of saving its original value. */
2706 /* RA was copied to another register or (before any function call) may
2707 still be in the original RA register. This is not always reliable:
2708 even in a leaf function, register tracking stops after prologue, and
2709 even in prologue, non-prologue instructions (not tracked) may overwrite
2710 RA or any register it was copied to. If likely in prologue or before
2711 any call, use retracking info and hope for the best (compiler should
2712 have saved RA in stack if not in a leaf function). If not in prologue,
2718 && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
2720 if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
2724 ra = get_frame_register_unsigned
2726 gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
2731 finish_frame_analysis:
2732 cache->pc = start_pc;
2734 /* RA == 0 marks the outermost frame. Do not go past it. */
2735 cache->prev_sp = (ra != 0) ? prev_sp : 0;
2736 cache->c0.fp_regnum = fp_regnum;
2737 cache->c0.c0_frmsz = c0_frmsz;
2738 cache->c0.c0_hasfp = c0_hasfp;
2739 cache->c0.c0_fp = fp;
2742 static CORE_ADDR a0_saved;
2743 static CORE_ADDR a7_saved;
2744 static CORE_ADDR a11_saved;
2745 static int a0_was_saved;
2746 static int a7_was_saved;
2747 static int a11_was_saved;
2749 /* Simulate L32E instruction: AT <-- ref (AS + offset). */
2751 execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2753 int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
2754 int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
2755 CORE_ADDR addr = xtensa_read_register (asreg) + offset;
2756 unsigned int spilled_value
2757 = read_memory_unsigned_integer (addr, 4, gdbarch_byte_order (gdbarch));
2759 if ((at == 0) && !a0_was_saved)
2761 a0_saved = xtensa_read_register (atreg);
2764 else if ((at == 7) && !a7_was_saved)
2766 a7_saved = xtensa_read_register (atreg);
2769 else if ((at == 11) && !a11_was_saved)
2771 a11_saved = xtensa_read_register (atreg);
2775 xtensa_write_register (atreg, spilled_value);
2778 /* Simulate S32E instruction: AT --> ref (AS + offset). */
2780 execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2782 int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
2783 int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
2784 CORE_ADDR addr = xtensa_read_register (asreg) + offset;
2785 ULONGEST spilled_value = xtensa_read_register (atreg);
2787 write_memory_unsigned_integer (addr, 4,
2788 gdbarch_byte_order (gdbarch),
2792 #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN 200
2798 xtNoExceptionHandler
2799 } xtensa_exception_handler_t;
2801 /* Execute instruction stream from current PC until hitting RFWU or RFWO.
2802 Return type of Xtensa Window Interrupt Handler on success. */
2803 static xtensa_exception_handler_t
2804 execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
2807 xtensa_insnbuf ins, slot;
2808 char ibuf[XTENSA_ISA_BSZ];
2809 CORE_ADDR ia, bt, ba;
2811 int ilen, islots, is;
2815 void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);
2819 /* WindowUnderflow12 = true, when inside _WindowUnderflow12. */
2820 int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140;
2822 isa = xtensa_default_isa;
2823 gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2824 ins = xtensa_insnbuf_alloc (isa);
2825 slot = xtensa_insnbuf_alloc (isa);
2834 while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
2836 if (ia + xtensa_isa_maxlength (isa) > bt)
2839 bt = (ba + XTENSA_ISA_BSZ);
2840 if (target_read_memory (ba, ibuf, bt - ba) != 0)
2841 return xtNoExceptionHandler;
2843 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2844 ifmt = xtensa_format_decode (isa, ins);
2845 if (ifmt == XTENSA_UNDEFINED)
2846 return xtNoExceptionHandler;
2847 ilen = xtensa_format_length (isa, ifmt);
2848 if (ilen == XTENSA_UNDEFINED)
2849 return xtNoExceptionHandler;
2850 islots = xtensa_format_num_slots (isa, ifmt);
2851 if (islots == XTENSA_UNDEFINED)
2852 return xtNoExceptionHandler;
2853 for (is = 0; is < islots; ++is)
2855 if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
2856 return xtNoExceptionHandler;
2857 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2858 if (opc == XTENSA_UNDEFINED)
2859 return xtNoExceptionHandler;
2860 switch (call0_classify_opcode (isa, opc))
2866 /* We expect none of them here. */
2867 return xtNoExceptionHandler;
2869 func = execute_l32e;
2872 func = execute_s32e;
2874 case c0opc_rfwo: /* RFWO. */
2875 /* Here, we return from WindowOverflow handler and,
2876 if we stopped at the very beginning, which means
2877 A0 was saved, we have to restore it now. */
2880 int arreg = arreg_number (gdbarch,
2881 gdbarch_tdep (gdbarch)->a0_base,
2883 xtensa_write_register (arreg, a0_saved);
2885 return xtWindowOverflow;
2886 case c0opc_rfwu: /* RFWU. */
2887 /* Here, we return from WindowUnderflow handler.
2888 Let's see if either A7 or A11 has to be restored. */
2889 if (WindowUnderflow12)
2893 int arreg = arreg_number (gdbarch,
2894 gdbarch_tdep (gdbarch)->a0_base + 11,
2896 xtensa_write_register (arreg, a11_saved);
2899 else if (a7_was_saved)
2901 int arreg = arreg_number (gdbarch,
2902 gdbarch_tdep (gdbarch)->a0_base + 7,
2904 xtensa_write_register (arreg, a7_saved);
2906 return xtWindowUnderflow;
2907 default: /* Simply skip this insns. */
2911 /* Decode arguments for L32E / S32E and simulate their execution. */
2912 if ( xtensa_opcode_num_operands (isa, opc) != 3 )
2913 return xtNoExceptionHandler;
2914 if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, &at))
2915 return xtNoExceptionHandler;
2916 if (xtensa_operand_decode (isa, opc, 0, &at))
2917 return xtNoExceptionHandler;
2918 if (xtensa_operand_get_field (isa, opc, 1, ifmt, is, slot, &as))
2919 return xtNoExceptionHandler;
2920 if (xtensa_operand_decode (isa, opc, 1, &as))
2921 return xtNoExceptionHandler;
2922 if (xtensa_operand_get_field (isa, opc, 2, ifmt, is, slot, &offset))
2923 return xtNoExceptionHandler;
2924 if (xtensa_operand_decode (isa, opc, 2, &offset))
2925 return xtNoExceptionHandler;
2927 (*func) (gdbarch, at, as, offset, wb);
2932 return xtNoExceptionHandler;
2935 /* Handle Window Overflow / Underflow exception frames. */
2938 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
2939 xtensa_frame_cache_t *cache,
2942 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2943 CORE_ADDR ps, wb, ws, ra;
2944 int epc1_regnum, i, regnum;
2945 xtensa_exception_handler_t eh_type;
2947 /* Read PS, WB, and WS from the hardware. Note that PS register
2948 must be present, if Windowed ABI is supported. */
2949 ps = xtensa_read_register (gdbarch_ps_regnum (gdbarch));
2950 wb = xtensa_read_register (gdbarch_tdep (gdbarch)->wb_regnum);
2951 ws = xtensa_read_register (gdbarch_tdep (gdbarch)->ws_regnum);
2953 /* Execute all the remaining instructions from Window Interrupt Handler
2954 by simulating them on the remote protocol level. On return, set the
2955 type of Xtensa Window Interrupt Handler, or report an error. */
2956 eh_type = execute_code (gdbarch, pc, wb);
2957 if (eh_type == xtNoExceptionHandler)
2959 Unable to decode Xtensa Window Interrupt Handler's code."));
2961 cache->ps = ps ^ PS_EXC; /* Clear the exception bit in PS. */
2962 cache->call0 = 0; /* It's Windowed ABI. */
2964 /* All registers for the cached frame will be alive. */
2965 for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
2966 cache->wd.aregs[i] = -1;
2968 if (eh_type == xtWindowOverflow)
2969 cache->wd.ws = ws ^ (1 << wb);
2970 else /* eh_type == xtWindowUnderflow. */
2971 cache->wd.ws = ws | (1 << wb);
2973 cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB. */
2974 regnum = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base,
2976 ra = xtensa_read_register (regnum);
2977 cache->wd.callsize = WINSIZE (ra);
2978 cache->prev_sp = xtensa_read_register (regnum + 1);
2979 /* Set regnum to a frame pointer of the frame being cached. */
2980 regnum = xtensa_scan_prologue (gdbarch, pc);
2981 regnum = arreg_number (gdbarch,
2982 gdbarch_tdep (gdbarch)->a0_base + regnum,
2984 cache->base = get_frame_register_unsigned (this_frame, regnum);
2986 /* Read PC of interrupted function from EPC1 register. */
2987 epc1_regnum = xtensa_find_register_by_name (gdbarch,"epc1");
2988 if (epc1_regnum < 0)
2989 error(_("Unable to read Xtensa register EPC1"));
2990 cache->ra = xtensa_read_register (epc1_regnum);
2991 cache->pc = get_frame_func (this_frame);
2995 /* Skip function prologue.
2997 Return the pc of the first instruction after prologue. GDB calls this to
2998 find the address of the first line of the function or (if there is no line
2999 number information) to skip the prologue for planting breakpoints on
3000 function entries. Use debug info (if present) or prologue analysis to skip
3001 the prologue to achieve reliable debugging behavior. For windowed ABI,
3002 only the 'entry' instruction is skipped. It is not strictly necessary to
3003 skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
3004 backtrace at any point in the prologue, however certain potential hazards
3005 are avoided and a more "normal" debugging experience is ensured by
3006 skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
3007 For example, if we don't skip the prologue:
3008 - Some args may not yet have been saved to the stack where the debug
3009 info expects to find them (true anyway when only 'entry' is skipped);
3010 - Software breakpoints ('break' instrs) may not have been unplanted
3011 when the prologue analysis is done on initializing the frame cache,
3012 and breaks in the prologue will throw off the analysis.
3014 If we have debug info ( line-number info, in particular ) we simply skip
3015 the code associated with the first function line effectively skipping
3016 the prologue code. It works even in cases like
3019 { int local_var = 1;
3023 because, for this source code, both Xtensa compilers will generate two
3024 separate entries ( with the same line number ) in dwarf line-number
3025 section to make sure there is a boundary between the prologue code and
3026 the rest of the function.
3028 If there is no debug info, we need to analyze the code. */
3030 /* #define DONT_SKIP_PROLOGUE */
3033 xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
3035 struct symtab_and_line prologue_sal;
3038 DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
3040 #if DONT_SKIP_PROLOGUE
3044 /* Try to find first body line from debug info. */
3046 prologue_sal = find_pc_line (start_pc, 0);
3047 if (prologue_sal.line != 0) /* Found debug info. */
3049 /* In Call0, it is possible to have a function with only one instruction
3050 ('ret') resulting from a one-line optimized function that does nothing.
3051 In that case, prologue_sal.end may actually point to the start of the
3052 next function in the text section, causing a breakpoint to be set at
3053 the wrong place. Check, if the end address is within a different
3054 function, and if so return the start PC. We know we have symbol
3059 if ((gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
3060 && call0_ret (start_pc, prologue_sal.end))
3063 find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
3064 if (end_func != start_pc)
3067 return prologue_sal.end;
3070 /* No debug line info. Analyze prologue for Call0 or simply skip ENTRY. */
3071 body_pc = call0_analyze_prologue (gdbarch, start_pc, 0, 0,
3072 xtensa_alloc_frame_cache (0));
3073 return body_pc != 0 ? body_pc : start_pc;
3076 /* Verify the current configuration. */
3078 xtensa_verify_config (struct gdbarch *gdbarch)
3080 struct ui_file *log;
3081 struct cleanup *cleanups;
3082 struct gdbarch_tdep *tdep;
3086 tdep = gdbarch_tdep (gdbarch);
3087 log = mem_fileopen ();
3088 cleanups = make_cleanup_ui_file_delete (log);
3090 /* Verify that we got a reasonable number of AREGS. */
3091 if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
3092 fprintf_unfiltered (log, _("\
3093 \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
3096 /* Verify that certain registers exist. */
3098 if (tdep->pc_regnum == -1)
3099 fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
3100 if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
3101 fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));
3103 if (tdep->isa_use_windowed_registers)
3105 if (tdep->wb_regnum == -1)
3106 fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
3107 if (tdep->ws_regnum == -1)
3108 fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
3109 if (tdep->ar_base == -1)
3110 fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
3113 if (tdep->a0_base == -1)
3114 fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
3116 buf = ui_file_xstrdup (log, &length);
3117 make_cleanup (xfree, buf);
3119 internal_error (__FILE__, __LINE__,
3120 _("the following are invalid: %s"), buf);
3121 do_cleanups (cleanups);
3125 /* Derive specific register numbers from the array of registers. */
3128 xtensa_derive_tdep (struct gdbarch_tdep *tdep)
3130 xtensa_register_t* rmap;
3131 int n, max_size = 4;
3134 tdep->num_nopriv_regs = 0;
3136 /* Special registers 0..255 (core). */
3137 #define XTENSA_DBREGN_SREG(n) (0x0200+(n))
3139 for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
3141 if (rmap->target_number == 0x0020)
3142 tdep->pc_regnum = n;
3143 else if (rmap->target_number == 0x0100)
3145 else if (rmap->target_number == 0x0000)
3147 else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
3148 tdep->wb_regnum = n;
3149 else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
3150 tdep->ws_regnum = n;
3151 else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
3152 tdep->debugcause_regnum = n;
3153 else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
3154 tdep->exccause_regnum = n;
3155 else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
3156 tdep->excvaddr_regnum = n;
3157 else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
3158 tdep->lbeg_regnum = n;
3159 else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
3160 tdep->lend_regnum = n;
3161 else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
3162 tdep->lcount_regnum = n;
3163 else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
3164 tdep->sar_regnum = n;
3165 else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
3166 tdep->litbase_regnum = n;
3167 else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
3168 tdep->ps_regnum = n;
3170 else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
3171 tdep->interrupt_regnum = n;
3172 else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
3173 tdep->interrupt2_regnum = n;
3174 else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
3175 tdep->cpenable_regnum = n;
3178 if (rmap->byte_size > max_size)
3179 max_size = rmap->byte_size;
3180 if (rmap->mask != 0 && tdep->num_regs == 0)
3182 /* Find out out how to deal with priveleged registers.
3184 if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3185 && tdep->num_nopriv_regs == 0)
3186 tdep->num_nopriv_regs = n;
3188 if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3189 && tdep->num_regs == 0)
3193 /* Number of pseudo registers. */
3194 tdep->num_pseudo_regs = n - tdep->num_regs;
3196 /* Empirically determined maximum sizes. */
3197 tdep->max_register_raw_size = max_size;
3198 tdep->max_register_virtual_size = max_size;
3201 /* Module "constructor" function. */
3203 extern struct gdbarch_tdep xtensa_tdep;
3205 static struct gdbarch *
3206 xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3208 struct gdbarch_tdep *tdep;
3209 struct gdbarch *gdbarch;
3210 struct xtensa_abi_handler *abi_handler;
3212 DEBUGTRACE ("gdbarch_init()\n");
3214 /* We have to set the byte order before we call gdbarch_alloc. */
3215 info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
3217 tdep = &xtensa_tdep;
3218 gdbarch = gdbarch_alloc (&info, tdep);
3219 xtensa_derive_tdep (tdep);
3221 /* Verify our configuration. */
3222 xtensa_verify_config (gdbarch);
3223 xtensa_session_once_reported = 0;
3225 /* Pseudo-Register read/write. */
3226 set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
3227 set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
3229 /* Set target information. */
3230 set_gdbarch_num_regs (gdbarch, tdep->num_regs);
3231 set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
3232 set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
3233 set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
3234 set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
3236 /* Renumber registers for known formats (stabs and dwarf2). */
3237 set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
3238 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
3240 /* We provide our own function to get register information. */
3241 set_gdbarch_register_name (gdbarch, xtensa_register_name);
3242 set_gdbarch_register_type (gdbarch, xtensa_register_type);
3244 /* To call functions from GDB using dummy frame. */
3245 set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
3247 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
3249 set_gdbarch_return_value (gdbarch, xtensa_return_value);
3251 /* Advance PC across any prologue instructions to reach "real" code. */
3252 set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
3254 /* Stack grows downward. */
3255 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3257 /* Set breakpoints. */
3258 set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
3260 /* After breakpoint instruction or illegal instruction, pc still
3261 points at break instruction, so don't decrement. */
3262 set_gdbarch_decr_pc_after_break (gdbarch, 0);
3264 /* We don't skip args. */
3265 set_gdbarch_frame_args_skip (gdbarch, 0);
3267 set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
3269 set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
3271 set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
3273 /* Frame handling. */
3274 frame_base_set_default (gdbarch, &xtensa_frame_base);
3275 frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
3276 dwarf2_append_unwinders (gdbarch);
3278 set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
3280 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
3282 xtensa_add_reggroups (gdbarch);
3283 set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
3285 set_gdbarch_regset_from_core_section (gdbarch,
3286 xtensa_regset_from_core_section);
3288 set_solib_svr4_fetch_link_map_offsets
3289 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
3295 xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3297 error (_("xtensa_dump_tdep(): not implemented"));
3300 /* Provide a prototype to silence -Wmissing-prototypes. */
3301 extern initialize_file_ftype _initialize_xtensa_tdep;
3304 _initialize_xtensa_tdep (void)
3306 struct cmd_list_element *c;
3308 gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
3309 xtensa_init_reggroups ();
3311 add_setshow_zinteger_cmd ("xtensa",
3313 &xtensa_debug_level,
3314 _("Set Xtensa debugging."),
3315 _("Show Xtensa debugging."), _("\
3316 When non-zero, Xtensa-specific debugging is enabled. \
3317 Can be 1, 2, 3, or 4 indicating the level of debugging."),
3320 &setdebuglist, &showdebuglist);