1 /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
2 
3    Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
20 #include "defs.h"
21 #include "frame.h"
22 #include "solib-svr4.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "gdbtypes.h"
27 #include "gdbcore.h"
28 #include "value.h"
29 #include "dis-asm.h"
30 #include "inferior.h"
31 #include "floatformat.h"
32 #include "regcache.h"
33 #include "reggroups.h"
34 #include "regset.h"
35 
36 #include "dummy-frame.h"
37 #include "dwarf2.h"
38 #include "dwarf2-frame.h"
39 #include "dwarf2loc.h"
40 #include "frame.h"
41 #include "frame-base.h"
42 #include "frame-unwind.h"
43 
44 #include "arch-utils.h"
45 #include "gdbarch.h"
46 #include "remote.h"
47 #include "serial.h"
48 
49 #include "command.h"
50 #include "gdbcmd.h"
51 #include "gdb_assert.h"
52 
53 #include "xtensa-isa.h"
54 #include "xtensa-tdep.h"
55 #include "xtensa-config.h"
56 
57 
58 static unsigned int xtensa_debug_level = 0;
59 
60 #define DEBUGWARN(args...) \
61   if (xtensa_debug_level > 0) \
62     fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
63 
64 #define DEBUGINFO(args...) \
65   if (xtensa_debug_level > 1) \
66     fprintf_unfiltered (gdb_stdlog, "(info ) " args)
67 
68 #define DEBUGTRACE(args...) \
69   if (xtensa_debug_level > 2) \
70     fprintf_unfiltered (gdb_stdlog, "(trace) " args)
71 
72 #define DEBUGVERB(args...) \
73   if (xtensa_debug_level > 3) \
74     fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
75 
76 
77 /* According to the ABI, the SP must be aligned to 16-byte boundaries.  */
78 #define SP_ALIGNMENT 16
79 
80 
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
85 
86 
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))
92 
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.  */
96 #define TX_PS			0x20
97 
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))
106 
107 /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
108    indicates that the instruction is an ENTRY instruction.  */
109 
110 #define XTENSA_IS_ENTRY(gdbarch, op1) \
111   ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
112    ? ((op1) == 0x6c) : ((op1) == 0x36))
113 
114 #define XTENSA_ENTRY_LENGTH	3
115 
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.  */
119 
120 #define PS_WOE			(1<<18)
121 #define PS_EXC			(1<<4)
122 
123 static int
windowing_enabled(struct gdbarch * gdbarch,unsigned int ps)124 windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
125 {
126   /* If we know CALL0 ABI is set explicitly,  say it is Call0.  */
127   if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
128     return 0;
129 
130   return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
131 }
132 
133 /* Convert a live A-register number to the corresponding AR-register
134    number.  */
135 static int
arreg_number(struct gdbarch * gdbarch,int a_regnum,ULONGEST wb)136 arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
137 {
138   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
139   int arreg;
140 
141   arreg = a_regnum - tdep->a0_base;
142   arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
143   arreg &= tdep->num_aregs - 1;
144 
145   return arreg + tdep->ar_base;
146 }
147 
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.  */
150 static int
areg_number(struct gdbarch * gdbarch,int ar_regnum,unsigned int wb)151 areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
152 {
153   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
154   int areg;
155 
156   areg = ar_regnum - tdep->ar_base;
157   if (areg < 0 || areg >= tdep->num_aregs)
158     return -1;
159   areg = (areg - wb * 4) & (tdep->num_aregs - 1);
160   return (areg > 15) ? -1 : areg;
161 }
162 
163 /* Read Xtensa register directly from the hardware.  */
164 static unsigned long
xtensa_read_register(int regnum)165 xtensa_read_register (int regnum)
166 {
167   ULONGEST value;
168 
169   regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
170   return (unsigned long) value;
171 }
172 
173 /* Write Xtensa register directly to the hardware.  */
174 static void
xtensa_write_register(int regnum,ULONGEST value)175 xtensa_write_register (int regnum, ULONGEST value)
176 {
177   regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
178 }
179 
180 /* Return the window size of the previous call to the function from which we
181    have just returned.
182 
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.  */
190 
191 static int
extract_call_winsize(struct gdbarch * gdbarch,CORE_ADDR pc)192 extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
193 {
194   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
195   int winsize = 4;
196   int insn;
197   gdb_byte buf[4];
198 
199   DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
200 
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);
204 
205   /* Decode call instruction:
206      Little Endian
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
209      Big Endian
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.  */
212 
213   if (byte_order == BFD_ENDIAN_LITTLE)
214     {
215       if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
216 	winsize = (insn & 0x30) >> 2;   /* 0, 4, 8, 12.  */
217     }
218   else
219     {
220       if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
221 	winsize = (insn >> 16) & 0xc;   /* 0, 4, 8, 12.  */
222     }
223   return winsize;
224 }
225 
226 
227 /* REGISTER INFORMATION */
228 
229 /* Find register by name.  */
230 static int
xtensa_find_register_by_name(struct gdbarch * gdbarch,char * name)231 xtensa_find_register_by_name (struct gdbarch *gdbarch, char *name)
232 {
233   int i;
234 
235   for (i = 0; i < gdbarch_num_regs (gdbarch)
236 	 + gdbarch_num_pseudo_regs (gdbarch);
237        i++)
238 
239     if (strcasecmp (gdbarch_tdep (gdbarch)->regmap[i].name, name) == 0)
240       return i;
241 
242   return -1;
243 }
244 
245 /* Returns the name of a register.  */
246 static const char *
xtensa_register_name(struct gdbarch * gdbarch,int regnum)247 xtensa_register_name (struct gdbarch *gdbarch, int regnum)
248 {
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;
253 
254   internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
255   return 0;
256 }
257 
258 /* Return the type of a register.  Create a new type, if necessary.  */
259 
260 static struct type *
xtensa_register_type(struct gdbarch * gdbarch,int regnum)261 xtensa_register_type (struct gdbarch *gdbarch, int regnum)
262 {
263   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
264 
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;
271 
272   if (regnum == gdbarch_pc_regnum (gdbarch)
273       || regnum == tdep->a0_base + 1)
274     return builtin_type (gdbarch)->builtin_data_ptr;
275 
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))
279     {
280       xtensa_register_t* reg = &tdep->regmap[regnum];
281 
282       /* Set ctype for this register (only the first time).  */
283 
284       if (reg->ctype == 0)
285 	{
286 	  struct ctype_cache *tp;
287 	  int size = reg->byte_size;
288 
289 	  /* We always use the memory representation,
290 	     even if the register width is smaller.  */
291 	  switch (size)
292 	    {
293 	    case 1:
294 	      reg->ctype = builtin_type (gdbarch)->builtin_uint8;
295 	      break;
296 
297 	    case 2:
298 	      reg->ctype = builtin_type (gdbarch)->builtin_uint16;
299 	      break;
300 
301 	    case 4:
302 	      reg->ctype = builtin_type (gdbarch)->builtin_uint32;
303 	      break;
304 
305 	    case 8:
306 	      reg->ctype = builtin_type (gdbarch)->builtin_uint64;
307 	      break;
308 
309 	    case 16:
310 	      reg->ctype = builtin_type (gdbarch)->builtin_uint128;
311 	      break;
312 
313 	    default:
314 	      for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
315 		if (tp->size == size)
316 		  break;
317 
318 	      if (tp == NULL)
319 		{
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;
324 		  tp->size = size;
325 		  tp->virtual_type
326 		    = arch_integer_type (gdbarch, size * 8, 1, name);
327 		  xfree (name);
328 		}
329 
330 	      reg->ctype = tp->virtual_type;
331 	    }
332 	}
333       return reg->ctype;
334     }
335 
336   internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
337   return 0;
338 }
339 
340 
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.  */
344 
345 static int
xtensa_reg_to_regnum(struct gdbarch * gdbarch,int regnum)346 xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
347 {
348   int i;
349 
350   if (regnum >= 0 && regnum < 16)
351     return gdbarch_tdep (gdbarch)->a0_base + regnum;
352 
353   for (i = 0;
354        i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
355        i++)
356     if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
357       return i;
358 
359   internal_error (__FILE__, __LINE__,
360 		  _("invalid dwarf/stabs register number %d"), regnum);
361   return 0;
362 }
363 
364 
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.  */
369 
370 static void
xtensa_register_write_masked(struct regcache * regcache,xtensa_register_t * reg,const gdb_byte * buffer)371 xtensa_register_write_masked (struct regcache *regcache,
372 			      xtensa_register_t *reg, const gdb_byte *buffer)
373 {
374   unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
375   const xtensa_mask_t *mask = reg->mask;
376 
377   int shift = 0;		/* Shift for next mask (mod 32).  */
378   int start, size;		/* Start bit and size of current mask.  */
379 
380   unsigned int *ptr = value;
381   unsigned int regval, m, mem = 0;
382 
383   int bytesize = reg->byte_size;
384   int bitsize = bytesize * 8;
385   int i, r;
386 
387   DEBUGTRACE ("xtensa_register_write_masked ()\n");
388 
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++)
392       {
393 	mem >>= 8;
394 	mem |= (buffer[bytesize - i - 1] << 24);
395 	if ((i & 3) == 3)
396 	  *ptr++ = mem;
397       }
398   else
399     for (i = 0; i < bytesize; i++)
400       {
401 	mem >>= 8;
402 	mem |= (buffer[i] << 24);
403 	if ((i & 3) == 3)
404 	  *ptr++ = mem;
405       }
406 
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.  */
410 
411   *ptr = mem >> (((0 - bytesize) & 3) * 8);
412   ptr = value;
413   mem = *ptr;
414 
415   /* Write the bits to the masked areas of the other registers.  */
416   for (i = 0; i < mask->count; i++)
417     {
418       start = mask->mask[i].bit_start;
419       size = mask->mask[i].bit_size;
420       regval = mem >> shift;
421 
422       if ((shift += size) > bitsize)
423 	error (_("size of all masks is larger than the register"));
424 
425       if (shift >= 32)
426 	{
427 	  mem = *(++ptr);
428 	  shift -= 32;
429 	  bitsize -= 32;
430 
431 	  if (shift > 0)
432 	    regval |= mem << (size - shift);
433 	}
434 
435       /* Make sure we have a valid register.  */
436       r = mask->mask[i].reg_num;
437       if (r >= 0 && size > 0)
438 	{
439 	  /* Don't overwrite the unmasked areas.  */
440 	  ULONGEST old_val;
441 	  regcache_cooked_read_unsigned (regcache, r, &old_val);
442 	  m = 0xffffffff >> (32 - size) << start;
443 	  regval <<= start;
444 	  regval = (regval & m) | (old_val & ~m);
445 	  regcache_cooked_write_unsigned (regcache, r, regval);
446 	}
447     }
448 }
449 
450 
451 /* Read a tie state or mapped registers.  Read the masked areas
452    of the registers and assemble them into a single value.  */
453 
454 static enum register_status
xtensa_register_read_masked(struct regcache * regcache,xtensa_register_t * reg,gdb_byte * buffer)455 xtensa_register_read_masked (struct regcache *regcache,
456 			     xtensa_register_t *reg, gdb_byte *buffer)
457 {
458   unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
459   const xtensa_mask_t *mask = reg->mask;
460 
461   int shift = 0;
462   int start, size;
463 
464   unsigned int *ptr = value;
465   unsigned int regval, mem = 0;
466 
467   int bytesize = reg->byte_size;
468   int bitsize = bytesize * 8;
469   int i;
470 
471   DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
472 	      reg->name == 0 ? "" : reg->name);
473 
474   /* Assemble the register from the masked areas of other registers.  */
475   for (i = 0; i < mask->count; i++)
476     {
477       int r = mask->mask[i].reg_num;
478       if (r >= 0)
479 	{
480 	  enum register_status status;
481 	  ULONGEST val;
482 
483 	  status = regcache_cooked_read_unsigned (regcache, r, &val);
484 	  if (status != REG_VALID)
485 	    return status;
486 	  regval = (unsigned int) val;
487 	}
488       else
489 	regval = 0;
490 
491       start = mask->mask[i].bit_start;
492       size = mask->mask[i].bit_size;
493 
494       regval >>= start;
495 
496       if (size < 32)
497 	regval &= (0xffffffff >> (32 - size));
498 
499       mem |= regval << shift;
500 
501       if ((shift += size) > bitsize)
502 	error (_("size of all masks is larger than the register"));
503 
504       if (shift >= 32)
505 	{
506 	  *ptr++ = mem;
507 	  bitsize -= 32;
508 	  shift -= 32;
509 
510 	  if (shift == 0)
511 	    mem = 0;
512 	  else
513 	    mem = regval >> (size - shift);
514 	}
515     }
516 
517   if (shift > 0)
518     *ptr = mem;
519 
520   /* Copy value to target byte order.  */
521   ptr = value;
522   mem = *ptr;
523 
524   if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
525     for (i = 0; i < bytesize; i++)
526       {
527 	if ((i & 3) == 0)
528 	  mem = *ptr++;
529 	buffer[bytesize - i - 1] = mem & 0xff;
530 	mem >>= 8;
531       }
532   else
533     for (i = 0; i < bytesize; i++)
534       {
535 	if ((i & 3) == 0)
536 	  mem = *ptr++;
537 	buffer[i] = mem & 0xff;
538 	mem >>= 8;
539       }
540 
541   return REG_VALID;
542 }
543 
544 
545 /* Read pseudo registers.  */
546 
547 static enum register_status
xtensa_pseudo_register_read(struct gdbarch * gdbarch,struct regcache * regcache,int regnum,gdb_byte * buffer)548 xtensa_pseudo_register_read (struct gdbarch *gdbarch,
549 			     struct regcache *regcache,
550 			     int regnum,
551 			     gdb_byte *buffer)
552 {
553   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
554 
555   DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
556 	      regnum, xtensa_register_name (gdbarch, regnum));
557 
558   if (regnum == gdbarch_num_regs (gdbarch)
559 		+ gdbarch_num_pseudo_regs (gdbarch) - 1)
560      regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
561 
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))
566     {
567       gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
568       enum register_status status;
569 
570       status = regcache_raw_read (regcache,
571 				  gdbarch_tdep (gdbarch)->wb_regnum,
572 				  buf);
573       if (status != REG_VALID)
574 	return status;
575       regnum = arreg_number (gdbarch, regnum,
576 			     extract_unsigned_integer (buf, 4, byte_order));
577     }
578 
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);
582 
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.  */
585 
586   else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
587     {
588       buffer[0] = (gdb_byte)0;
589       buffer[1] = (gdb_byte)0;
590       buffer[2] = (gdb_byte)0;
591       buffer[3] = (gdb_byte)0;
592       return REG_VALID;
593     }
594   /* Pseudo registers.  */
595   else if (regnum >= 0
596 	    && regnum < gdbarch_num_regs (gdbarch)
597 			+ gdbarch_num_pseudo_regs (gdbarch))
598     {
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;
602 
603       /* We cannot read Unknown or Unmapped registers.  */
604       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
605 	{
606 	  if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
607 	    {
608 	      warning (_("cannot read register %s"),
609 		       xtensa_register_name (gdbarch, regnum));
610 	      return REG_VALID;
611 	    }
612 	}
613 
614       /* Some targets cannot read TIE register files.  */
615       else if (type == xtRegisterTypeTieRegfile)
616         {
617 	  /* Use 'fetch' to get register?  */
618 	  if (flags & xtTargetFlagsUseFetchStore)
619 	    {
620 	      warning (_("cannot read register"));
621 	      return REG_VALID;
622 	    }
623 
624 	  /* On some targets (esp. simulators), we can always read the reg.  */
625 	  else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
626 	    {
627 	      warning (_("cannot read register"));
628 	      return REG_VALID;
629 	    }
630 	}
631 
632       /* We can always read mapped registers.  */
633       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
634 	return xtensa_register_read_masked (regcache, reg, buffer);
635 
636       /* Assume that we can read the register.  */
637       return regcache_raw_read (regcache, regnum, buffer);
638     }
639   else
640     internal_error (__FILE__, __LINE__,
641 		    _("invalid register number %d"), regnum);
642 }
643 
644 
645 /* Write pseudo registers.  */
646 
647 static void
xtensa_pseudo_register_write(struct gdbarch * gdbarch,struct regcache * regcache,int regnum,const gdb_byte * buffer)648 xtensa_pseudo_register_write (struct gdbarch *gdbarch,
649 			      struct regcache *regcache,
650 			      int regnum,
651 			      const gdb_byte *buffer)
652 {
653   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
654 
655   DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
656 	      regnum, xtensa_register_name (gdbarch, regnum));
657 
658   if (regnum == gdbarch_num_regs (gdbarch)
659 		+ gdbarch_num_pseudo_regs (gdbarch) -1)
660      regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
661 
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))
666     {
667       gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
668 
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));
673     }
674 
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);
679 
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.  */
682 
683   else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
684     {
685       return;
686     }
687   /* Pseudo registers.  */
688   else if (regnum >= 0
689 	   && regnum < gdbarch_num_regs (gdbarch)
690 		       + gdbarch_num_pseudo_regs (gdbarch))
691     {
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;
695 
696       /* On most targets, we cannot write registers
697 	 of type "Unknown" or "Unmapped".  */
698       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
699         {
700 	  if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
701 	    {
702 	      warning (_("cannot write register %s"),
703 		       xtensa_register_name (gdbarch, regnum));
704 	      return;
705 	    }
706 	}
707 
708       /* Some targets cannot read TIE register files.  */
709       else if (type == xtRegisterTypeTieRegfile)
710         {
711 	  /* Use 'store' to get register?  */
712 	  if (flags & xtTargetFlagsUseFetchStore)
713 	    {
714 	      warning (_("cannot write register"));
715 	      return;
716 	    }
717 
718 	  /* On some targets (esp. simulators), we can always write
719 	     the register.  */
720 	  else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
721 	    {
722 	      warning (_("cannot write register"));
723 	      return;
724 	    }
725 	}
726 
727       /* We can always write mapped registers.  */
728       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
729         {
730 	  xtensa_register_write_masked (regcache, reg, buffer);
731 	  return;
732 	}
733 
734       /* Assume that we can write the register.  */
735       regcache_raw_write (regcache, regnum, buffer);
736     }
737   else
738     internal_error (__FILE__, __LINE__,
739 		    _("invalid register number %d"), regnum);
740 }
741 
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];
746 
747 static void
xtensa_init_reggroups(void)748 xtensa_init_reggroups (void)
749 {
750   int i;
751   char cpname[] = "cp0";
752 
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);
756 
757   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
758     {
759       cpname[2] = '0' + i;
760       xtensa_cp[i] = reggroup_new (cpname, USER_REGGROUP);
761     }
762 }
763 
764 static void
xtensa_add_reggroups(struct gdbarch * gdbarch)765 xtensa_add_reggroups (struct gdbarch *gdbarch)
766 {
767   int i;
768 
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);
777 
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);
782 
783   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
784     reggroup_add (gdbarch, xtensa_cp[i]);
785 }
786 
787 static int
xtensa_coprocessor_register_group(struct reggroup * group)788 xtensa_coprocessor_register_group (struct reggroup *group)
789 {
790   int i;
791 
792   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
793     if (group == xtensa_cp[i])
794       return i;
795 
796   return -1;
797 }
798 
799 #define SAVE_REST_FLAGS	(XTENSA_REGISTER_FLAGS_READABLE \
800 			| XTENSA_REGISTER_FLAGS_WRITABLE \
801 			| XTENSA_REGISTER_FLAGS_VOLATILE)
802 
803 #define SAVE_REST_VALID	(XTENSA_REGISTER_FLAGS_READABLE \
804 			| XTENSA_REGISTER_FLAGS_WRITABLE)
805 
806 static int
xtensa_register_reggroup_p(struct gdbarch * gdbarch,int regnum,struct reggroup * group)807 xtensa_register_reggroup_p (struct gdbarch *gdbarch,
808 			    int regnum,
809     			    struct reggroup *group)
810 {
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;
814   int cp_number;
815 
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.  */
819     return 1;
820 
821   /* First, skip registers that are not visible to this target
822      (unknown and unmapped registers when not using ISS).  */
823 
824   if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
825     return 0;
826   if (group == all_reggroup)
827     return 1;
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);
844   if (cp_number >= 0)
845     return rg & (xtRegisterGroupCP0 << cp_number);
846   else
847     return 1;
848 }
849 
850 
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.  */
854 
855 static void
xtensa_supply_gregset(const struct regset * regset,struct regcache * rc,int regnum,const void * gregs,size_t len)856 xtensa_supply_gregset (const struct regset *regset,
857 		       struct regcache *rc,
858 		       int regnum,
859 		       const void *gregs,
860 		       size_t len)
861 {
862   const xtensa_elf_gregset_t *regs = gregs;
863   struct gdbarch *gdbarch = get_regcache_arch (rc);
864   int i;
865 
866   DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);
867 
868   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
869     regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) &regs->pc);
870   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
871     regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) &regs->ps);
872   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
873     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
874 			 (char *) &regs->windowbase);
875   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
876     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
877 			 (char *) &regs->windowstart);
878   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
879     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
880 			 (char *) &regs->lbeg);
881   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
882     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
883 			 (char *) &regs->lend);
884   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
885     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
886 			 (char *) &regs->lcount);
887   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
888     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
889 			 (char *) &regs->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 *) &regs->ar[regnum - gdbarch_tdep
895 			   (gdbarch)->ar_base]);
896   else if (regnum == -1)
897     {
898       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
899 	regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
900 			     (char *) &regs->ar[i]);
901     }
902 }
903 
904 
905 /* Xtensa register set.  */
906 
907 static struct regset
908 xtensa_gregset =
909 {
910   NULL,
911   xtensa_supply_gregset
912 };
913 
914 
915 /* Return the appropriate register set for the core
916    section identified by SECT_NAME and SECT_SIZE.  */
917 
918 static const struct regset *
xtensa_regset_from_core_section(struct gdbarch * core_arch,const char * sect_name,size_t sect_size)919 xtensa_regset_from_core_section (struct gdbarch *core_arch,
920 				 const char *sect_name,
921 				 size_t sect_size)
922 {
923   DEBUGTRACE ("xtensa_regset_from_core_section "
924 	      "(..., sect_name==\"%s\", sect_size==%x)\n",
925 	      sect_name, (unsigned int) sect_size);
926 
927   if (strcmp (sect_name, ".reg") == 0
928       && sect_size >= sizeof(xtensa_elf_gregset_t))
929     return &xtensa_gregset;
930 
931   return NULL;
932 }
933 
934 
935 /* Handling frames.  */
936 
937 /* Number of registers to save in case of Windowed ABI.  */
938 #define XTENSA_NUM_SAVED_AREGS		12
939 
940 /* Frame cache part for Windowed ABI.  */
941 typedef struct xtensa_windowed_frame_cache
942 {
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.  */
949 
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;
954 
955 /* Call0 ABI Definitions.  */
956 
957 #define C0_MAXOPDS  3	/* Maximum number of operands for prologue
958 			   analysis.  */
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.  */
966 
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.  */
973 
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.  */
977 
978 extern xtensa_isa xtensa_default_isa;
979 
980 typedef struct xtensa_c0reg
981 {
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.  */
987 } xtensa_c0reg_t;
988 
989 /* Frame cache part for Call0 ABI.  */
990 typedef struct xtensa_call0_frame_cache
991 {
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.  */
1004 
1005   xtensa_c0reg_t c0_rt[C0_NREGS];  /* Register tracking information.  */
1006 } xtensa_call0_frame_cache_t;
1007 
1008 typedef struct xtensa_frame_cache
1009 {
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).  */
1016   union
1017     {
1018       xtensa_windowed_frame_cache_t	wd;	/* call0 == false.  */
1019       xtensa_call0_frame_cache_t       	c0;	/* call0 == true.  */
1020     };
1021 } xtensa_frame_cache_t;
1022 
1023 
1024 static struct xtensa_frame_cache *
xtensa_alloc_frame_cache(int windowed)1025 xtensa_alloc_frame_cache (int windowed)
1026 {
1027   xtensa_frame_cache_t *cache;
1028   int i;
1029 
1030   DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
1031 
1032   cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
1033 
1034   cache->base = 0;
1035   cache->pc = 0;
1036   cache->ra = 0;
1037   cache->ps = 0;
1038   cache->prev_sp = 0;
1039   cache->call0 = !windowed;
1040   if (cache->call0)
1041     {
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;
1049 
1050       for (i = 0; i < C0_NREGS; i++)
1051 	{
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;
1055 	}
1056     }
1057   else
1058     {
1059       cache->wd.wb = 0;
1060       cache->wd.ws = 0;
1061       cache->wd.callsize = -1;
1062 
1063       for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
1064 	cache->wd.aregs[i] = -1;
1065     }
1066   return cache;
1067 }
1068 
1069 
1070 static CORE_ADDR
xtensa_frame_align(struct gdbarch * gdbarch,CORE_ADDR address)1071 xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
1072 {
1073   return address & ~15;
1074 }
1075 
1076 
1077 static CORE_ADDR
xtensa_unwind_pc(struct gdbarch * gdbarch,struct frame_info * next_frame)1078 xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1079 {
1080   gdb_byte buf[8];
1081   CORE_ADDR pc;
1082 
1083   DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n",
1084 		host_address_to_string (next_frame));
1085 
1086   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1087   pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1088 
1089   DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
1090 
1091   return pc;
1092 }
1093 
1094 
1095 static struct frame_id
xtensa_dummy_id(struct gdbarch * gdbarch,struct frame_info * this_frame)1096 xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1097 {
1098   CORE_ADDR pc, fp;
1099 
1100   /* THIS-FRAME is a dummy frame.  Return a frame ID of that frame.  */
1101 
1102   pc = get_frame_pc (this_frame);
1103   fp = get_frame_register_unsigned
1104 	 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1105 
1106   /* Make dummy frame ID unique by adding a constant.  */
1107   return frame_id_build (fp + SP_ALIGNMENT, pc);
1108 }
1109 
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.  */
1112 
1113 static int
xtensa_window_interrupt_insn(struct gdbarch * gdbarch,CORE_ADDR pc)1114 xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
1115 {
1116   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1117   unsigned int insn = read_memory_integer (pc, 4, byte_order);
1118   unsigned int code;
1119 
1120   if (byte_order == BFD_ENDIAN_BIG)
1121     {
1122       /* Check, if this is L32E or S32E.  */
1123       code = insn & 0xf000ff00;
1124       if ((code == 0x00009000) || (code == 0x00009400))
1125 	return 1;
1126       /* Check, if this is RFWU or RFWO.  */
1127       code = insn & 0xffffff00;
1128       return ((code == 0x00430000) || (code == 0x00530000));
1129     }
1130   else
1131     {
1132       /* Check, if this is L32E or S32E.  */
1133       code = insn & 0x00ff000f;
1134       if ((code == 0x090000) || (code == 0x490000))
1135 	return 1;
1136       /* Check, if this is RFWU or RFWO.  */
1137       code = insn & 0x00ffffff;
1138       return ((code == 0x00003400) || (code == 0x00003500));
1139     }
1140 }
1141 
1142 /* Returns the best guess about which register is a frame pointer
1143    for the function containing CURRENT_PC.  */
1144 
1145 #define XTENSA_ISA_BSZ		32		/* Instruction buffer size.  */
1146 #define XTENSA_ISA_BADPC	((CORE_ADDR)0)	/* Bad PC value.  */
1147 
1148 static unsigned int
xtensa_scan_prologue(struct gdbarch * gdbarch,CORE_ADDR current_pc)1149 xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
1150 {
1151 #define RETURN_FP goto done
1152 
1153   unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
1154   CORE_ADDR start_addr;
1155   xtensa_isa isa;
1156   xtensa_insnbuf ins, slot;
1157   char ibuf[XTENSA_ISA_BSZ];
1158   CORE_ADDR ia, bt, ba;
1159   xtensa_format ifmt;
1160   int ilen, islots, is;
1161   xtensa_opcode opc;
1162   const char *opcname;
1163 
1164   find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
1165   if (start_addr == 0)
1166     return fp_regnum;
1167 
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);
1174   ba = 0;
1175 
1176   for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
1177     {
1178       if (ia + xtensa_isa_maxlength (isa) > bt)
1179         {
1180 	  ba = ia;
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)
1184 	    RETURN_FP;
1185 	}
1186 
1187       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
1188       ifmt = xtensa_format_decode (isa, ins);
1189       if (ifmt == XTENSA_UNDEFINED)
1190 	RETURN_FP;
1191       ilen = xtensa_format_length (isa, ifmt);
1192       if (ilen == XTENSA_UNDEFINED)
1193 	RETURN_FP;
1194       islots = xtensa_format_num_slots (isa, ifmt);
1195       if (islots == XTENSA_UNDEFINED)
1196 	RETURN_FP;
1197 
1198       for (is = 0; is < islots; ++is)
1199 	{
1200 	  if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
1201 	    RETURN_FP;
1202 
1203 	  opc = xtensa_opcode_decode (isa, ifmt, is, slot);
1204 	  if (opc == XTENSA_UNDEFINED)
1205 	    RETURN_FP;
1206 
1207 	  opcname = xtensa_opcode_name (isa, opc);
1208 
1209 	  if (strcasecmp (opcname, "mov.n") == 0
1210 	      || strcasecmp (opcname, "or") == 0)
1211 	    {
1212 	      unsigned int register_operand;
1213 
1214 	      /* Possible candidate for setting frame pointer
1215 		 from A1.  This is what we are looking for.  */
1216 
1217 	      if (xtensa_operand_get_field (isa, opc, 1, ifmt,
1218 					    is, slot, &register_operand) != 0)
1219 		RETURN_FP;
1220 	      if (xtensa_operand_decode (isa, opc, 1, &register_operand) != 0)
1221 		RETURN_FP;
1222 	      if (register_operand == 1)  /* Mov{.n} FP A1.  */
1223 		{
1224 		  if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
1225 						&register_operand) != 0)
1226 		    RETURN_FP;
1227 		  if (xtensa_operand_decode (isa, opc, 0,
1228 					     &register_operand) != 0)
1229 		    RETURN_FP;
1230 
1231 		  fp_regnum
1232 		    = gdbarch_tdep (gdbarch)->a0_base + register_operand;
1233 		  RETURN_FP;
1234 		}
1235 	    }
1236 
1237 	  if (
1238 	      /* We have problems decoding the memory.  */
1239 	      opcname == NULL
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.  */
1253 	    RETURN_FP;
1254 	}
1255     }
1256 done:
1257   xtensa_insnbuf_free(isa, slot);
1258   xtensa_insnbuf_free(isa, ins);
1259   return fp_regnum;
1260 }
1261 
1262 /* The key values to identify the frame using "cache" are
1263 
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.  */
1267 
1268 static void
1269 call0_frame_cache (struct frame_info *this_frame,
1270 		   xtensa_frame_cache_t *cache, CORE_ADDR pc);
1271 
1272 static void
1273 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
1274 				     xtensa_frame_cache_t *cache,
1275 				     CORE_ADDR pc);
1276 
1277 static struct xtensa_frame_cache *
xtensa_frame_cache(struct frame_info * this_frame,void ** this_cache)1278 xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
1279 {
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;
1286 
1287   if (*this_cache)
1288     return *this_cache;
1289 
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);
1294 
1295   windowed = windowing_enabled (gdbarch, ps);
1296 
1297   /* Get pristine xtensa-frame.  */
1298   cache = xtensa_alloc_frame_cache (windowed);
1299   *this_cache = cache;
1300 
1301   if (windowed)
1302     {
1303       char op1;
1304 
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);
1310 
1311       op1 = read_memory_integer (pc, 1, byte_order);
1312       if (XTENSA_IS_ENTRY (gdbarch, op1))
1313 	{
1314 	  int callinc = CALLINC (ps);
1315 	  ra = get_frame_register_unsigned
1316 	    (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
1317 
1318 	  /* ENTRY hasn't been executed yet, therefore callsize is still 0.  */
1319 	  cache->wd.callsize = 0;
1320 	  cache->wd.wb = wb;
1321 	  cache->wd.ws = ws;
1322 	  cache->prev_sp = get_frame_register_unsigned
1323 			     (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1324 
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;
1330 
1331 	  cache->pc = pc;
1332 	  cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1333 	  cache->ps = (ps & ~PS_CALLINC_MASK)
1334 	    | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1335 
1336 	  return cache;
1337 	}
1338       else
1339 	{
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);
1347 
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);
1352 	}
1353 
1354       if (cache->wd.ws == 0)
1355 	{
1356 	  int i;
1357 
1358 	  /* Set A0...A3.  */
1359 	  sp = get_frame_register_unsigned
1360 	    (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
1361 
1362 	  for (i = 0; i < 4; i++, sp += 4)
1363 	    {
1364 	      cache->wd.aregs[i] = sp;
1365 	    }
1366 
1367 	  if (cache->wd.callsize > 4)
1368 	    {
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;
1375 
1376 	      for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
1377 		{
1378 		  cache->wd.aregs[i] = sp;
1379 		}
1380 	    }
1381 	}
1382 
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.  */
1386 	{
1387 	  if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
1388 	    {
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);
1394 	    }
1395 	  else
1396 	    {
1397 	      /* Read caller's frame SP directly from the previous window.  */
1398 	      int regnum = arreg_number
1399 			     (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
1400 			      cache->wd.wb);
1401 
1402 	      cache->prev_sp = xtensa_read_register (regnum);
1403 	    }
1404 	}
1405     }
1406   else if (xtensa_window_interrupt_insn (gdbarch, pc))
1407     {
1408       /* Execution stopped inside Xtensa Window Interrupt Handler.  */
1409 
1410       xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
1411       /* Everything was set already,  including cache->base.  */
1412       return cache;
1413     }
1414   else	/* Call0 framework.  */
1415     {
1416       call0_frame_cache (this_frame, cache, pc);
1417       fp_regnum = cache->c0.fp_regnum;
1418     }
1419 
1420   cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
1421 
1422   return cache;
1423 }
1424 
1425 static int xtensa_session_once_reported = 1;
1426 
1427 /* Report a problem with prologue analysis while doing backtracing.
1428    But, do it only once to avoid annoyng repeated messages.  */
1429 
1430 static void
warning_once(void)1431 warning_once (void)
1432 {
1433   if (xtensa_session_once_reported == 0)
1434     warning (_("\
1435 \nUnrecognised function prologue. Stack trace cannot be resolved. \
1436 This message will not be repeated in this session.\n"));
1437 
1438   xtensa_session_once_reported = 1;
1439 }
1440 
1441 
1442 static void
xtensa_frame_this_id(struct frame_info * this_frame,void ** this_cache,struct frame_id * this_id)1443 xtensa_frame_this_id (struct frame_info *this_frame,
1444 		      void **this_cache,
1445 		      struct frame_id *this_id)
1446 {
1447   struct xtensa_frame_cache *cache =
1448     xtensa_frame_cache (this_frame, this_cache);
1449 
1450   if (cache->prev_sp == 0)
1451     return;
1452 
1453   (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
1454 }
1455 
1456 static struct value *
xtensa_frame_prev_register(struct frame_info * this_frame,void ** this_cache,int regnum)1457 xtensa_frame_prev_register (struct frame_info *this_frame,
1458 			    void **this_cache,
1459 			    int regnum)
1460 {
1461   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1462   struct xtensa_frame_cache *cache;
1463   ULONGEST saved_reg = 0;
1464   int done = 1;
1465 
1466   if (*this_cache == NULL)
1467     *this_cache = xtensa_frame_cache (this_frame, this_cache);
1468   cache = *this_cache;
1469 
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)
1475     {
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;
1482       else
1483 	done = 0;
1484     }
1485   else
1486     done = 0;
1487 
1488   if (done)
1489     return frame_unwind_got_constant (this_frame, regnum, saved_reg);
1490 
1491   if (!cache->call0) /* Windowed ABI.  */
1492     {
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);
1498 
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))
1503 	{
1504 	  int areg = areg_number (gdbarch, regnum, cache->wd.wb);
1505 
1506 	  if (areg >= 0
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]);
1511 	}
1512     }
1513   else /* Call0 ABI.  */
1514     {
1515       int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
1516 		&& regnum <= (gdbarch_tdep (gdbarch)->ar_base
1517 			       + C0_NREGS))
1518 		  ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
1519 
1520       if (reg < C0_NREGS)
1521 	{
1522 	  CORE_ADDR spe;
1523 	  int stkofs;
1524 
1525 	  /* If register was saved in the prologue, retrieve it.  */
1526 	  stkofs = cache->c0.c0_rt[reg].to_stk;
1527 	  if (stkofs != C0_NOSTK)
1528 	    {
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;
1532 
1533 	      return frame_unwind_got_memory (this_frame, regnum,
1534 					      spe + stkofs);
1535 	    }
1536 	}
1537     }
1538 
1539   /* All other registers have been either saved to
1540      the stack or are still alive in the processor.  */
1541 
1542   return frame_unwind_got_register (this_frame, regnum, regnum);
1543 }
1544 
1545 
1546 static const struct frame_unwind
1547 xtensa_unwind =
1548 {
1549   NORMAL_FRAME,
1550   default_frame_unwind_stop_reason,
1551   xtensa_frame_this_id,
1552   xtensa_frame_prev_register,
1553   NULL,
1554   default_frame_sniffer
1555 };
1556 
1557 static CORE_ADDR
xtensa_frame_base_address(struct frame_info * this_frame,void ** this_cache)1558 xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
1559 {
1560   struct xtensa_frame_cache *cache =
1561     xtensa_frame_cache (this_frame, this_cache);
1562 
1563   return cache->base;
1564 }
1565 
1566 static const struct frame_base
1567 xtensa_frame_base =
1568 {
1569   &xtensa_unwind,
1570   xtensa_frame_base_address,
1571   xtensa_frame_base_address,
1572   xtensa_frame_base_address
1573 };
1574 
1575 
1576 static void
xtensa_extract_return_value(struct type * type,struct regcache * regcache,void * dst)1577 xtensa_extract_return_value (struct type *type,
1578 			     struct regcache *regcache,
1579 			     void *dst)
1580 {
1581   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1582   bfd_byte *valbuf = dst;
1583   int len = TYPE_LENGTH (type);
1584   ULONGEST pc, wb;
1585   int callsize, areg;
1586   int offset = 0;
1587 
1588   DEBUGTRACE ("xtensa_extract_return_value (...)\n");
1589 
1590   gdb_assert(len > 0);
1591 
1592   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1593     {
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);
1597 
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"),
1602 			len);
1603 
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);
1610     }
1611   else
1612     {
1613       /* No windowing hardware - Call0 ABI.  */
1614       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1615     }
1616 
1617   DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
1618 
1619   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1620     offset = 4 - len;
1621 
1622   for (; len > 0; len -= 4, areg++, valbuf += 4)
1623     {
1624       if (len < 4)
1625 	regcache_raw_read_part (regcache, areg, offset, len, valbuf);
1626       else
1627 	regcache_raw_read (regcache, areg, valbuf);
1628     }
1629 }
1630 
1631 
1632 static void
xtensa_store_return_value(struct type * type,struct regcache * regcache,const void * dst)1633 xtensa_store_return_value (struct type *type,
1634 			   struct regcache *regcache,
1635 			   const void *dst)
1636 {
1637   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1638   const bfd_byte *valbuf = dst;
1639   unsigned int areg;
1640   ULONGEST pc, wb;
1641   int callsize;
1642   int len = TYPE_LENGTH (type);
1643   int offset = 0;
1644 
1645   DEBUGTRACE ("xtensa_store_return_value (...)\n");
1646 
1647   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1648     {
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);
1653 
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);
1660 
1661       DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
1662               callsize, (int) wb);
1663     }
1664   else
1665     {
1666       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1667     }
1668 
1669   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1670     offset = 4 - len;
1671 
1672   for (; len > 0; len -= 4, areg++, valbuf += 4)
1673     {
1674       if (len < 4)
1675 	regcache_raw_write_part (regcache, areg, offset, len, valbuf);
1676       else
1677 	regcache_raw_write (regcache, areg, valbuf);
1678     }
1679 }
1680 
1681 
1682 static enum return_value_convention
xtensa_return_value(struct gdbarch * gdbarch,struct value * function,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)1683 xtensa_return_value (struct gdbarch *gdbarch,
1684 		     struct value *function,
1685 		     struct type *valtype,
1686 		     struct regcache *regcache,
1687 		     gdb_byte *readbuf,
1688 		     const gdb_byte *writebuf)
1689 {
1690   /* Structures up to 16 bytes are returned in registers.  */
1691 
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);
1696 
1697   if (struct_return)
1698     return RETURN_VALUE_STRUCT_CONVENTION;
1699 
1700   DEBUGTRACE ("xtensa_return_value(...)\n");
1701 
1702   if (writebuf != NULL)
1703     {
1704       xtensa_store_return_value (valtype, regcache, writebuf);
1705     }
1706 
1707   if (readbuf != NULL)
1708     {
1709       gdb_assert (!struct_return);
1710       xtensa_extract_return_value (valtype, regcache, readbuf);
1711     }
1712   return RETURN_VALUE_REGISTER_CONVENTION;
1713 }
1714 
1715 
1716 /* DUMMY FRAME */
1717 
1718 static CORE_ADDR
xtensa_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,int struct_return,CORE_ADDR struct_addr)1719 xtensa_push_dummy_call (struct gdbarch *gdbarch,
1720 			struct value *function,
1721 			struct regcache *regcache,
1722 			CORE_ADDR bp_addr,
1723 			int nargs,
1724 			struct value **args,
1725 			CORE_ADDR sp,
1726 			int struct_return,
1727 			CORE_ADDR struct_addr)
1728 {
1729   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1730   int i;
1731   int size, onstack_size;
1732   gdb_byte *buf = (gdb_byte *) alloca (16);
1733   CORE_ADDR ra, ps;
1734   struct argument_info
1735   {
1736     const bfd_byte *contents;
1737     int length;
1738     int onstack;		/* onstack == 0 => in reg */
1739     int align;			/* alignment */
1740     union
1741     {
1742       int offset;		/* stack offset if on stack.  */
1743       int regno;		/* regno if in register.  */
1744     } u;
1745   };
1746 
1747   struct argument_info *arg_info =
1748     (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
1749 
1750   CORE_ADDR osp = sp;
1751 
1752   DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
1753 
1754   if (xtensa_debug_level > 3)
1755     {
1756       int i;
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);
1761 
1762       for (i = 0; i < nargs; i++)
1763         {
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))
1770 	    {
1771 	    case TYPE_CODE_INT:
1772 	      fprintf_unfiltered (gdb_stdlog, "int");
1773 	      break;
1774 	    case TYPE_CODE_STRUCT:
1775 	      fprintf_unfiltered (gdb_stdlog, "struct");
1776 	      break;
1777 	    default:
1778 	      fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
1779 	      break;
1780 	    }
1781 	  fprintf_unfiltered (gdb_stdlog, " %s\n",
1782 			      host_address_to_string (value_contents (arg)));
1783 	}
1784     }
1785 
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.  */
1790 
1791   size = 0;
1792   onstack_size = 0;
1793   i = 0;
1794 
1795   if (struct_return)
1796     size = REGISTER_SIZE;
1797 
1798   for (i = 0; i < nargs; i++)
1799     {
1800       struct argument_info *info = &arg_info[i];
1801       struct value *arg = args[i];
1802       struct type *arg_type = check_typedef (value_type (arg));
1803 
1804       switch (TYPE_CODE (arg_type))
1805 	{
1806 	case TYPE_CODE_INT:
1807 	case TYPE_CODE_BOOL:
1808 	case TYPE_CODE_CHAR:
1809 	case TYPE_CODE_RANGE:
1810 	case TYPE_CODE_ENUM:
1811 
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))
1815 	    {
1816 	      arg_type = builtin_type (gdbarch)->builtin_long;
1817 	      arg = value_cast (arg_type, arg);
1818 	    }
1819 	  /* Aligment is equal to the type length for the basic types.  */
1820 	  info->align = TYPE_LENGTH (arg_type);
1821 	  break;
1822 
1823 	case TYPE_CODE_FLT:
1824 
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);
1829 	  else
1830 	    info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1831 	  break;
1832 
1833 	case TYPE_CODE_STRUCT:
1834 	default:
1835 	  info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1836 	  break;
1837 	}
1838       info->length = TYPE_LENGTH (arg_type);
1839       info->contents = value_contents (arg);
1840 
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);
1844 
1845       if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
1846 	{
1847 	  info->onstack = 1;
1848 	  info->u.offset = onstack_size;
1849 	  onstack_size += info->length;
1850 	}
1851       else
1852 	{
1853 	  info->onstack = 0;
1854 	  info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
1855 	}
1856       size += info->length;
1857     }
1858 
1859   /* Adjust the stack pointer and align it.  */
1860   sp = align_down (sp - onstack_size, SP_ALIGNMENT);
1861 
1862   /* Simulate MOVSP, if Windowed ABI.  */
1863   if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1864       && (sp != osp))
1865     {
1866       read_memory (osp - 16, buf, 16);
1867       write_memory (sp - 16, buf, 16);
1868     }
1869 
1870   /* Second Loop: Load arguments.  */
1871 
1872   if (struct_return)
1873     {
1874       store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
1875       regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
1876     }
1877 
1878   for (i = 0; i < nargs; i++)
1879     {
1880       struct argument_info *info = &arg_info[i];
1881 
1882       if (info->onstack)
1883 	{
1884 	  int n = info->length;
1885 	  CORE_ADDR offset = sp + info->u.offset;
1886 
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.  */
1890 
1891 	  if (n < REGISTER_SIZE
1892 	      && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1893 	    offset += (REGISTER_SIZE - n);
1894 
1895 	  write_memory (offset, info->contents, info->length);
1896 
1897 	}
1898       else
1899 	{
1900 	  int n = info->length;
1901 	  const bfd_byte *cp = info->contents;
1902 	  int r = info->u.regno;
1903 
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.  */
1909 
1910 	  if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
1911 	    {
1912 	      ULONGEST v;
1913 	      v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
1914 	      v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
1915 
1916 	      store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
1917 	      regcache_cooked_write (regcache, r, buf);
1918 
1919 	      cp += REGISTER_SIZE;
1920 	      n -= REGISTER_SIZE;
1921 	      r++;
1922 	    }
1923 	  else
1924 	    while (n > 0)
1925 	      {
1926 		regcache_cooked_write (regcache, r, cp);
1927 
1928 		cp += REGISTER_SIZE;
1929 		n -= REGISTER_SIZE;
1930 		r++;
1931 	      }
1932 	}
1933     }
1934 
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.  */
1938 
1939   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1940     {
1941       ULONGEST val;
1942 
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),
1950 				      ps | 0x00010000);
1951 
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.  */
1956 
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));
1961     }
1962   else
1963     {
1964       /* Simulate CALL0: write RA into A0 register.  */
1965       regcache_cooked_write_unsigned
1966 	(regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
1967     }
1968 
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;
1974 }
1975 
1976 
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.  */
1980 
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 }
1985 
1986 static const unsigned char *
xtensa_breakpoint_from_pc(struct gdbarch * gdbarch,CORE_ADDR * pcptr,int * lenptr)1987 xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1988 			   int *lenptr)
1989 {
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;
1994 
1995   DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
1996 
1997   if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
1998     {
1999       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2000 	{
2001 	  *lenptr = sizeof (density_big_breakpoint);
2002 	  return density_big_breakpoint;
2003 	}
2004       else
2005 	{
2006 	  *lenptr = sizeof (density_little_breakpoint);
2007 	  return density_little_breakpoint;
2008 	}
2009     }
2010   else
2011     {
2012       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2013 	{
2014 	  *lenptr = sizeof (big_breakpoint);
2015 	  return big_breakpoint;
2016 	}
2017       else
2018 	{
2019 	  *lenptr = sizeof (little_breakpoint);
2020 	  return little_breakpoint;
2021 	}
2022     }
2023 }
2024 
2025 /* Call0 ABI support routines.  */
2026 
2027 /* Return true, if PC points to "ret" or "ret.n".  */
2028 
2029 static int
call0_ret(CORE_ADDR start_pc,CORE_ADDR finish_pc)2030 call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
2031 {
2032 #define RETURN_RET goto done
2033   xtensa_isa isa;
2034   xtensa_insnbuf ins, slot;
2035   char ibuf[XTENSA_ISA_BSZ];
2036   CORE_ADDR ia, bt, ba;
2037   xtensa_format ifmt;
2038   int ilen, islots, is;
2039   xtensa_opcode opc;
2040   const char *opcname;
2041   int found_ret = 0;
2042 
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);
2047   ba = 0;
2048 
2049   for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
2050     {
2051       if (ia + xtensa_isa_maxlength (isa) > bt)
2052         {
2053 	  ba = ia;
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 )
2057 	    RETURN_RET;
2058 	}
2059 
2060       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2061       ifmt = xtensa_format_decode (isa, ins);
2062       if (ifmt == XTENSA_UNDEFINED)
2063 	RETURN_RET;
2064       ilen = xtensa_format_length (isa, ifmt);
2065       if (ilen == XTENSA_UNDEFINED)
2066 	RETURN_RET;
2067       islots = xtensa_format_num_slots (isa, ifmt);
2068       if (islots == XTENSA_UNDEFINED)
2069 	RETURN_RET;
2070 
2071       for (is = 0; is < islots; ++is)
2072 	{
2073 	  if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
2074 	    RETURN_RET;
2075 
2076 	  opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2077 	  if (opc == XTENSA_UNDEFINED)
2078 	    RETURN_RET;
2079 
2080 	  opcname = xtensa_opcode_name (isa, opc);
2081 
2082 	  if ((strcasecmp (opcname, "ret.n") == 0)
2083 	      || (strcasecmp (opcname, "ret") == 0))
2084 	    {
2085 	      found_ret = 1;
2086 	      RETURN_RET;
2087 	    }
2088 	}
2089     }
2090  done:
2091   xtensa_insnbuf_free(isa, slot);
2092   xtensa_insnbuf_free(isa, ins);
2093   return found_ret;
2094 }
2095 
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.  */
2100 
2101 typedef enum
2102 {
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.  */
2122 } xtensa_insn_kind;
2123 
2124 /* Return true,  if OPCNAME is RSR,  WRS,  or XSR instruction.  */
2125 
2126 static int
rwx_special_register(const char * opcname)2127 rwx_special_register (const char *opcname)
2128 {
2129   char ch = *opcname++;
2130 
2131   if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
2132     return 0;
2133   if (*opcname++ != 's')
2134     return 0;
2135   if (*opcname++ != 'r')
2136     return 0;
2137   if (*opcname++ != '.')
2138     return 0;
2139 
2140   return 1;
2141 }
2142 
2143 /* Classify an opcode based on what it means for Call0 prologue analysis.  */
2144 
2145 static xtensa_insn_kind
call0_classify_opcode(xtensa_isa isa,xtensa_opcode opc)2146 call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
2147 {
2148   const char *opcname;
2149   xtensa_insn_kind opclass = c0opc_uninteresting;
2150 
2151   DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
2152 
2153   /* Get opcode name and handle special classifications.  */
2154 
2155   opcname = xtensa_opcode_name (isa, opc);
2156 
2157   if (opcname == NULL
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;
2177 
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;
2207 
2208   return opclass;
2209 }
2210 
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
2216    otherwise.  */
2217 
2218 static int
call0_track_op(struct gdbarch * gdbarch,xtensa_c0reg_t dst[],xtensa_c0reg_t src[],xtensa_insn_kind opclass,int nods,unsigned odv[],CORE_ADDR pc,int spreg,xtensa_frame_cache_t * cache)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)
2222 {
2223   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2224   unsigned litbase, litaddr, litval;
2225 
2226   switch (opclass)
2227     {
2228     case c0opc_addi:
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];
2233       break;
2234     case c0opc_add:
2235       /* 3 operands: dst, src1, src2.  */
2236       gdb_assert (nods == 3);
2237       if      (src[odv[1]].fr_reg == C0_CONST)
2238         {
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;
2241 	}
2242       else if (src[odv[2]].fr_reg == C0_CONST)
2243         {
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;
2246 	}
2247       else dst[odv[0]].fr_reg = C0_INEXP;
2248       break;
2249     case c0opc_and:
2250       /* 3 operands:  dst, src1, src2.  */
2251       gdb_assert (nods == 3);
2252       if (cache->c0.c0_fpalign == 0)
2253 	{
2254 	  /* Handle dynamic stack alignment.  */
2255 	  if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
2256 	    {
2257 	      if (src[odv[2]].fr_reg == C0_CONST)
2258 		cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
2259 	      break;
2260 	    }
2261 	  else if ((src[odv[0]].fr_reg == spreg)
2262 		   && (src[odv[2]].fr_reg == spreg))
2263 	    {
2264 	      if (src[odv[1]].fr_reg == C0_CONST)
2265 		cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
2266 	      break;
2267 	    }
2268 	  /* else fall through.  */
2269 	}
2270       if      (src[odv[1]].fr_reg == C0_CONST)
2271         {
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;
2274 	}
2275       else if (src[odv[2]].fr_reg == C0_CONST)
2276         {
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;
2279 	}
2280       else dst[odv[0]].fr_reg = C0_INEXP;
2281       break;
2282     case c0opc_sub:
2283       /* 3 operands: dst, src1, src2.  */
2284       gdb_assert (nods == 3);
2285       if      (src[odv[2]].fr_reg == C0_CONST)
2286         {
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;
2289 	}
2290       else dst[odv[0]].fr_reg = C0_INEXP;
2291       break;
2292     case c0opc_mov:
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];
2301 
2302       dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2303       dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
2304       break;
2305     case c0opc_movi:
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];
2310       break;
2311     case c0opc_l32r:
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;
2324       break;
2325     case c0opc_s32i:
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];
2334 
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.  */
2340         {
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];
2344 	}
2345       break;
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.  */
2349     case c0opc_l32e:
2350     case c0opc_s32e:
2351     case c0opc_rfwo:
2352     case c0opc_rfwu:
2353       return 1;
2354     default:
2355       return 1;
2356     }
2357   return 0;
2358 }
2359 
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.
2370 
2371    Returns:   Address of first instruction after prologue, or PC (whichever
2372 	      is first), or 0, if decoding failed (in libisa).
2373    Input args:
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.
2379    InOut args:
2380       cache   Xtensa frame cache.
2381 
2382       Note that these may produce useful results even if decoding fails
2383       because they begin with default assumptions that analysis may change.  */
2384 
2385 static CORE_ADDR
call0_analyze_prologue(struct gdbarch * gdbarch,CORE_ADDR start,CORE_ADDR pc,int nregs,xtensa_frame_cache_t * cache)2386 call0_analyze_prologue (struct gdbarch *gdbarch,
2387 			CORE_ADDR start, CORE_ADDR pc,
2388 			int nregs, xtensa_frame_cache_t *cache)
2389 {
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.  */
2407 
2408   struct symtab_and_line prologue_sal;
2409 
2410   DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
2411 	      (int)start, (int)pc);
2412 
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.  */
2420 
2421   rtmp = NULL;
2422   body_pc = UINT_MAX;
2423   end_pc = 0;
2424 
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;
2429 
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.  */
2432   if (pc == 0)
2433     {
2434       find_pc_partial_function (start, 0, NULL, &end_pc);
2435       body_pc = min (end_pc, body_pc);
2436     }
2437   else
2438     body_pc = min (pc, body_pc);
2439 
2440   cache->call0 = 1;
2441   rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
2442 
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);
2449 
2450   for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
2451     {
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).  */
2455 
2456       if (ia + xtensa_isa_maxlength (isa) > bt)
2457         {
2458 	  ba = ia;
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 ..."));
2462 	}
2463 
2464       /* Decode format information.  */
2465 
2466       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2467       ifmt = xtensa_format_decode (isa, ins);
2468       if (ifmt == XTENSA_UNDEFINED)
2469 	{
2470 	  fail = 1;
2471 	  goto done;
2472 	}
2473       ilen = xtensa_format_length (isa, ifmt);
2474       if (ilen == XTENSA_UNDEFINED)
2475 	{
2476 	  fail = 1;
2477 	  goto done;
2478 	}
2479       islots = xtensa_format_num_slots (isa, ifmt);
2480       if (islots == XTENSA_UNDEFINED)
2481 	{
2482 	  fail = 1;
2483 	  goto done;
2484 	}
2485 
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.  */
2489 
2490       for (j = 0; j < nregs; ++j)
2491 	rtmp[j] = cache->c0.c0_rt[j];
2492 
2493       for (is = 0; is < islots; ++is)
2494         {
2495 	  /* Decode a slot and classify the opcode.  */
2496 
2497 	  fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
2498 	  if (fail)
2499 	    goto done;
2500 
2501 	  opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2502 	  DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
2503 		     (unsigned)ia, opc);
2504 	  if (opc == XTENSA_UNDEFINED)
2505 	    opclass = c0opc_illegal;
2506 	  else
2507 	    opclass = call0_classify_opcode (isa, opc);
2508 
2509 	  /* Decide whether to track this opcode, ignore it, or bail out.  */
2510 
2511 	  switch (opclass)
2512 	    {
2513 	    case c0opc_illegal:
2514 	    case c0opc_break:
2515 	      fail = 1;
2516 	      goto done;
2517 
2518 	    case c0opc_uninteresting:
2519 	      continue;
2520 
2521 	    case c0opc_flow:  /* Flow control instructions stop analysis.  */
2522 	    case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis.  */
2523 	      goto done;
2524 
2525 	    case c0opc_entry:
2526 	      cache->call0 = 0;
2527 	      ia += ilen;	       	/* Skip over 'entry' insn.  */
2528 	      goto done;
2529 
2530 	    default:
2531 	      cache->call0 = 1;
2532 	    }
2533 
2534 	  /* Only expected opcodes should get this far.  */
2535 
2536 	  /* Extract and decode the operands.  */
2537 	  nods = xtensa_opcode_num_operands (isa, opc);
2538 	  if (nods == XTENSA_UNDEFINED)
2539 	    {
2540 	      fail = 1;
2541 	      goto done;
2542 	    }
2543 
2544 	  for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
2545 	    {
2546 	      fail = xtensa_operand_get_field (isa, opc, j, ifmt,
2547 					       is, slot, &odv[j]);
2548 	      if (fail)
2549 		goto done;
2550 
2551 	      fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
2552 	      if (fail)
2553 		goto done;
2554 	    }
2555 
2556 	  /* Check operands to verify use of 'mov' assembler macro.  */
2557 	  if (opclass == c0opc_mov && nods == 3)
2558 	    {
2559 	      if (odv[2] == odv[1])
2560 		{
2561 		  nods = 2;
2562 		  if ((odv[0] == 1) && (odv[1] != 1))
2563 		    /* OR  A1, An, An  , where n != 1.
2564 		       This means we are inside epilogue already.  */
2565 		    goto done;
2566 		}
2567 	      else
2568 		{
2569 		  opclass = c0opc_uninteresting;
2570 		  continue;
2571 		}
2572 	    }
2573 
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);
2577 	  if (fail)
2578 	    goto done;
2579 	}
2580     }
2581 done:
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;
2587 }
2588 
2589 /* Initialize frame cache for the current frame in CALL0 ABI.  */
2590 
2591 static void
call0_frame_cache(struct frame_info * this_frame,xtensa_frame_cache_t * cache,CORE_ADDR pc)2592 call0_frame_cache (struct frame_info *this_frame,
2593 		   xtensa_frame_cache_t *cache, CORE_ADDR pc)
2594 {
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;
2601 
2602   sp = get_frame_register_unsigned
2603     (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
2604   fp = sp; /* Assume FP == SP until proven otherwise.  */
2605 
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.  */
2608 
2609   if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
2610     {
2611       body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);
2612 
2613       if (body_pc == XTENSA_ISA_BADPC)
2614 	{
2615 	  warning_once ();
2616 	  ra = 0;
2617 	  goto finish_frame_analysis;
2618 	}
2619     }
2620 
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.  */
2626 
2627   if (body_pc <= pc)
2628     {
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;
2638     }
2639   else  /* No data from the prologue analysis.  */
2640     {
2641       c0_hasfp = 0;
2642       fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
2643       c0_frmsz = 0;
2644       start_pc = pc;
2645    }
2646 
2647   if (cache->c0.c0_fpalign)
2648     {
2649       /* This frame has a special prologue with a dynamic stack adjustment
2650 	 to force an alignment, which is bigger than standard 16 bytes.  */
2651 
2652       CORE_ADDR unaligned_sp;
2653 
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.  */
2657 	{
2658 	  warning_once ();
2659 	  ra = 0;
2660 	  goto finish_frame_analysis;
2661 	}
2662 
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);
2667       else
2668 	/* Get the value from stack.  */
2669 	unaligned_sp = (CORE_ADDR)
2670 	  read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);
2671 
2672       prev_sp = unaligned_sp + c0_frmsz;
2673     }
2674   else
2675     prev_sp = fp + c0_frmsz;
2676 
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.  */
2679   if (c0_hasfp)
2680     {
2681       fp = get_frame_register_unsigned (this_frame, fp_regnum);
2682 
2683       /* Update the stack frame size.  */
2684       c0_frmsz += fp - sp;
2685     }
2686 
2687   /* Get the return address (RA) from the stack if saved,
2688      or try to get it from a register.  */
2689 
2690   to_stk = cache->c0.c0_rt[C0_RA].to_stk;
2691   if (to_stk != C0_NOSTK)
2692     ra = (CORE_ADDR)
2693       read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
2694 			   4, byte_order);
2695 
2696   else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
2697 	   && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
2698     {
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.  */
2702       ra = 0;
2703     }
2704   else
2705     {
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,
2713 	 too bad.  */
2714 
2715       int i;
2716       for (i = 0;
2717 	   (i < C0_NREGS)
2718 	   && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
2719 	   ++i);
2720       if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
2721 	i = C0_RA;
2722       if (i < C0_NREGS)
2723 	{
2724 	  ra = get_frame_register_unsigned
2725 	    (this_frame,
2726 	     gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
2727 	}
2728       else ra = 0;
2729     }
2730 
2731  finish_frame_analysis:
2732   cache->pc = start_pc;
2733   cache->ra = ra;
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;
2740 }
2741 
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;
2748 
2749 /* Simulate L32E instruction:  AT <-- ref (AS + offset).  */
2750 static void
execute_l32e(struct gdbarch * gdbarch,int at,int as,int offset,CORE_ADDR wb)2751 execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2752 {
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));
2758 
2759   if ((at == 0) && !a0_was_saved)
2760     {
2761       a0_saved = xtensa_read_register (atreg);
2762       a0_was_saved = 1;
2763     }
2764   else if ((at == 7) && !a7_was_saved)
2765     {
2766       a7_saved = xtensa_read_register (atreg);
2767       a7_was_saved = 1;
2768     }
2769   else if ((at == 11) && !a11_was_saved)
2770     {
2771       a11_saved = xtensa_read_register (atreg);
2772       a11_was_saved = 1;
2773     }
2774 
2775   xtensa_write_register (atreg, spilled_value);
2776 }
2777 
2778 /* Simulate S32E instruction:  AT --> ref (AS + offset).  */
2779 static void
execute_s32e(struct gdbarch * gdbarch,int at,int as,int offset,CORE_ADDR wb)2780 execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2781 {
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);
2786 
2787   write_memory_unsigned_integer (addr, 4,
2788 				 gdbarch_byte_order (gdbarch),
2789 				 spilled_value);
2790 }
2791 
2792 #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN  200
2793 
2794 typedef enum
2795 {
2796   xtWindowOverflow,
2797   xtWindowUnderflow,
2798   xtNoExceptionHandler
2799 } xtensa_exception_handler_t;
2800 
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
execute_code(struct gdbarch * gdbarch,CORE_ADDR current_pc,CORE_ADDR wb)2804 execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
2805 {
2806   xtensa_isa isa;
2807   xtensa_insnbuf ins, slot;
2808   char ibuf[XTENSA_ISA_BSZ];
2809   CORE_ADDR ia, bt, ba;
2810   xtensa_format ifmt;
2811   int ilen, islots, is;
2812   xtensa_opcode opc;
2813   int insn_num = 0;
2814   int fail = 0;
2815   void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);
2816 
2817   int at, as, offset;
2818 
2819   /* WindowUnderflow12 = true, when inside _WindowUnderflow12.  */
2820   int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140;
2821 
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);
2826   ba = 0;
2827   ia = current_pc;
2828   bt = ia;
2829 
2830   a0_was_saved = 0;
2831   a7_was_saved = 0;
2832   a11_was_saved = 0;
2833 
2834   while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
2835     {
2836       if (ia + xtensa_isa_maxlength (isa) > bt)
2837         {
2838 	  ba = ia;
2839 	  bt = (ba + XTENSA_ISA_BSZ);
2840 	  if (target_read_memory (ba, ibuf, bt - ba) != 0)
2841 	    return xtNoExceptionHandler;
2842 	}
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)
2854 	{
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))
2861 	    {
2862 	    case c0opc_illegal:
2863 	    case c0opc_flow:
2864 	    case c0opc_entry:
2865 	    case c0opc_break:
2866 	      /* We expect none of them here.  */
2867 	      return xtNoExceptionHandler;
2868 	    case c0opc_l32e:
2869 	      func = execute_l32e;
2870 	      break;
2871 	    case c0opc_s32e:
2872 	      func = execute_s32e;
2873 	      break;
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.  */
2878 	      if (a0_was_saved)
2879 		{
2880 		  int arreg = arreg_number (gdbarch,
2881 					    gdbarch_tdep (gdbarch)->a0_base,
2882 					    wb);
2883 		  xtensa_write_register (arreg, a0_saved);
2884 		}
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)
2890 		{
2891 		  if (a11_was_saved)
2892 		    {
2893 		      int arreg = arreg_number (gdbarch,
2894 						gdbarch_tdep (gdbarch)->a0_base + 11,
2895 						wb);
2896 		      xtensa_write_register (arreg, a11_saved);
2897 		    }
2898 		}
2899 	      else if (a7_was_saved)
2900 		{
2901 		  int arreg = arreg_number (gdbarch,
2902 					    gdbarch_tdep (gdbarch)->a0_base + 7,
2903 					    wb);
2904 		  xtensa_write_register (arreg, a7_saved);
2905 		}
2906 	      return xtWindowUnderflow;
2907  	    default: /* Simply skip this insns.  */
2908 	      continue;
2909 	    }
2910 
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;
2926 
2927 	  (*func) (gdbarch, at, as, offset, wb);
2928 	}
2929 
2930       ia += ilen;
2931     }
2932   return xtNoExceptionHandler;
2933 }
2934 
2935 /* Handle Window Overflow / Underflow exception frames.  */
2936 
2937 static void
xtensa_window_interrupt_frame_cache(struct frame_info * this_frame,xtensa_frame_cache_t * cache,CORE_ADDR pc)2938 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
2939 				     xtensa_frame_cache_t *cache,
2940 				     CORE_ADDR pc)
2941 {
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;
2946 
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);
2952 
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)
2958     error (_("\
2959 Unable to decode Xtensa Window Interrupt Handler's code."));
2960 
2961   cache->ps = ps ^ PS_EXC;	/* Clear the exception bit in PS.  */
2962   cache->call0 = 0;		/* It's Windowed ABI.  */
2963 
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;
2967 
2968   if (eh_type == xtWindowOverflow)
2969     cache->wd.ws = ws ^ (1 << wb);
2970   else /* eh_type == xtWindowUnderflow.  */
2971     cache->wd.ws = ws | (1 << wb);
2972 
2973   cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB.  */
2974   regnum = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base,
2975 			 cache->wd.wb);
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,
2983 			 cache->wd.wb);
2984   cache->base = get_frame_register_unsigned (this_frame, regnum);
2985 
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);
2992 }
2993 
2994 
2995 /* Skip function prologue.
2996 
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.
3013 
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
3017 
3018    int main()
3019    {	int local_var = 1;
3020    	....
3021    }
3022 
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.
3027 
3028    If there is no debug info, we need to analyze the code.  */
3029 
3030 /* #define DONT_SKIP_PROLOGUE  */
3031 
3032 static CORE_ADDR
xtensa_skip_prologue(struct gdbarch * gdbarch,CORE_ADDR start_pc)3033 xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
3034 {
3035   struct symtab_and_line prologue_sal;
3036   CORE_ADDR body_pc;
3037 
3038   DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
3039 
3040 #if DONT_SKIP_PROLOGUE
3041   return start_pc;
3042 #endif
3043 
3044  /* Try to find first body line from debug info.  */
3045 
3046   prologue_sal = find_pc_line (start_pc, 0);
3047   if (prologue_sal.line != 0) /* Found debug info.  */
3048     {
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
3055 	 information.  */
3056 
3057       CORE_ADDR end_func;
3058 
3059       if ((gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
3060 	  && call0_ret (start_pc, prologue_sal.end))
3061 	return start_pc;
3062 
3063       find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
3064       if (end_func != start_pc)
3065 	return start_pc;
3066 
3067       return prologue_sal.end;
3068     }
3069 
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;
3074 }
3075 
3076 /* Verify the current configuration.  */
3077 static void
xtensa_verify_config(struct gdbarch * gdbarch)3078 xtensa_verify_config (struct gdbarch *gdbarch)
3079 {
3080   struct ui_file *log;
3081   struct cleanup *cleanups;
3082   struct gdbarch_tdep *tdep;
3083   long length;
3084   char *buf;
3085 
3086   tdep = gdbarch_tdep (gdbarch);
3087   log = mem_fileopen ();
3088   cleanups = make_cleanup_ui_file_delete (log);
3089 
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!"),
3094 			tdep->num_aregs);
3095 
3096   /* Verify that certain registers exist.  */
3097 
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"));
3102 
3103   if (tdep->isa_use_windowed_registers)
3104     {
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"));
3111     }
3112 
3113   if (tdep->a0_base == -1)
3114     fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
3115 
3116   buf = ui_file_xstrdup (log, &length);
3117   make_cleanup (xfree, buf);
3118   if (length > 0)
3119     internal_error (__FILE__, __LINE__,
3120 		    _("the following are invalid: %s"), buf);
3121   do_cleanups (cleanups);
3122 }
3123 
3124 
3125 /* Derive specific register numbers from the array of registers.  */
3126 
3127 static void
xtensa_derive_tdep(struct gdbarch_tdep * tdep)3128 xtensa_derive_tdep (struct gdbarch_tdep *tdep)
3129 {
3130   xtensa_register_t* rmap;
3131   int n, max_size = 4;
3132 
3133   tdep->num_regs = 0;
3134   tdep->num_nopriv_regs = 0;
3135 
3136 /* Special registers 0..255 (core).  */
3137 #define XTENSA_DBREGN_SREG(n)  (0x0200+(n))
3138 
3139   for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
3140     {
3141       if (rmap->target_number == 0x0020)
3142 	tdep->pc_regnum = n;
3143       else if (rmap->target_number == 0x0100)
3144 	tdep->ar_base = n;
3145       else if (rmap->target_number == 0x0000)
3146 	tdep->a0_base = n;
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;
3169 #if 0
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;
3176 #endif
3177 
3178       if (rmap->byte_size > max_size)
3179 	max_size = rmap->byte_size;
3180       if (rmap->mask != 0 && tdep->num_regs == 0)
3181 	tdep->num_regs = n;
3182       /* Find out out how to deal with priveleged registers.
3183 
3184          if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3185               && tdep->num_nopriv_regs == 0)
3186            tdep->num_nopriv_regs = n;
3187       */
3188       if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3189 	  && tdep->num_regs == 0)
3190 	tdep->num_regs = n;
3191     }
3192 
3193   /* Number of pseudo registers.  */
3194   tdep->num_pseudo_regs = n - tdep->num_regs;
3195 
3196   /* Empirically determined maximum sizes.  */
3197   tdep->max_register_raw_size = max_size;
3198   tdep->max_register_virtual_size = max_size;
3199 }
3200 
3201 /* Module "constructor" function.  */
3202 
3203 extern struct gdbarch_tdep xtensa_tdep;
3204 
3205 static struct gdbarch *
xtensa_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)3206 xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3207 {
3208   struct gdbarch_tdep *tdep;
3209   struct gdbarch *gdbarch;
3210   struct xtensa_abi_handler *abi_handler;
3211 
3212   DEBUGTRACE ("gdbarch_init()\n");
3213 
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;
3216 
3217   tdep = &xtensa_tdep;
3218   gdbarch = gdbarch_alloc (&info, tdep);
3219   xtensa_derive_tdep (tdep);
3220 
3221   /* Verify our configuration.  */
3222   xtensa_verify_config (gdbarch);
3223   xtensa_session_once_reported = 0;
3224 
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);
3228 
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);
3235 
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);
3239 
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);
3243 
3244   /* To call functions from GDB using dummy frame.  */
3245   set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
3246 
3247   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
3248 
3249   set_gdbarch_return_value (gdbarch, xtensa_return_value);
3250 
3251   /* Advance PC across any prologue instructions to reach "real" code.  */
3252   set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
3253 
3254   /* Stack grows downward.  */
3255   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3256 
3257   /* Set breakpoints.  */
3258   set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
3259 
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);
3263 
3264   /* We don't skip args.  */
3265   set_gdbarch_frame_args_skip (gdbarch, 0);
3266 
3267   set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
3268 
3269   set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
3270 
3271   set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
3272 
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);
3277 
3278   set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
3279 
3280   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
3281 
3282   xtensa_add_reggroups (gdbarch);
3283   set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
3284 
3285   set_gdbarch_regset_from_core_section (gdbarch,
3286 					xtensa_regset_from_core_section);
3287 
3288   set_solib_svr4_fetch_link_map_offsets
3289     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
3290 
3291   return gdbarch;
3292 }
3293 
3294 static void
xtensa_dump_tdep(struct gdbarch * gdbarch,struct ui_file * file)3295 xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3296 {
3297   error (_("xtensa_dump_tdep(): not implemented"));
3298 }
3299 
3300 /* Provide a prototype to silence -Wmissing-prototypes.  */
3301 extern initialize_file_ftype _initialize_xtensa_tdep;
3302 
3303 void
_initialize_xtensa_tdep(void)3304 _initialize_xtensa_tdep (void)
3305 {
3306   struct cmd_list_element *c;
3307 
3308   gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
3309   xtensa_init_reggroups ();
3310 
3311   add_setshow_zuinteger_cmd ("xtensa",
3312 			     class_maintenance,
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."),
3318 			     NULL,
3319 			     NULL,
3320 			     &setdebuglist, &showdebuglist);
3321 }
3322