OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-6.8/] [gdb/] [xtensa-tdep.c] - Blame information for rev 299

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
/* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
2
 
3
   Copyright (C) 2003, 2005, 2006, 2007, 2008 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 "symtab.h"
23
#include "symfile.h"
24
#include "objfiles.h"
25
#include "gdbtypes.h"
26
#include "gdbcore.h"
27
#include "value.h"
28
#include "dis-asm.h"
29
#include "inferior.h"
30
#include "floatformat.h"
31
#include "regcache.h"
32
#include "reggroups.h"
33
#include "regset.h"
34
 
35
#include "dummy-frame.h"
36
#include "elf/dwarf2.h"
37
#include "dwarf2-frame.h"
38
#include "dwarf2loc.h"
39
#include "frame.h"
40
#include "frame-base.h"
41
#include "frame-unwind.h"
42
 
43
#include "arch-utils.h"
44
#include "gdbarch.h"
45
#include "remote.h"
46
#include "serial.h"
47
 
48
#include "command.h"
49
#include "gdbcmd.h"
50
#include "gdb_assert.h"
51
 
52
#include "xtensa-isa.h"
53
#include "xtensa-tdep.h"
54
#include "xtensa-config.h"
55
 
56
 
57
static int xtensa_debug_level = 0;
58
 
59
#define DEBUGWARN(args...) \
60
  if (xtensa_debug_level > 0) \
61
    fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
62
 
63
#define DEBUGINFO(args...) \
64
  if (xtensa_debug_level > 1) \
65
    fprintf_unfiltered (gdb_stdlog, "(info ) " args)
66
 
67
#define DEBUGTRACE(args...) \
68
  if (xtensa_debug_level > 2) \
69
    fprintf_unfiltered (gdb_stdlog, "(trace) " args)
70
 
71
#define DEBUGVERB(args...) \
72
  if (xtensa_debug_level > 3) \
73
    fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
74
 
75
 
76
/* According to the ABI, the SP must be aligned to 16-byte boundaries.  */
77
#define SP_ALIGNMENT 16
78
 
79
 
80
/* On Windowed ABI, we use a6 through a11 for passing arguments
81
   to a function called by GDB because CALL4 is used.  */
82
#define ARGS_NUM_REGS           6
83
#define REGISTER_SIZE           4
84
 
85
 
86
/* Extract the call size from the return address or PS register.  */
87
#define PS_CALLINC_SHIFT        16
88
#define PS_CALLINC_MASK         0x00030000
89
#define CALLINC(ps)             (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
90
#define WINSIZE(ra)             (4 * (( (ra) >> 30) & 0x3))
91
 
92
/* ABI-independent macros.  */
93
#define ARG_NOF(gdbarch) \
94
  (gdbarch_tdep (gdbarch)->call_abi \
95
   == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
96
#define ARG_1ST(gdbarch) \
97
  (gdbarch_tdep (gdbarch)->call_abi  == CallAbiCall0Only \
98
   ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
99
   : (gdbarch_tdep (gdbarch)->a0_base + 6))
100
 
101
/* XTENSA_IS_ENTRY tests whether the first byte of an instruction
102
   indicates that the instruction is an ENTRY instruction.  */
103
 
104
#define XTENSA_IS_ENTRY(gdbarch, op1) \
105
  ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
106
   ? ((op1) == 0x6c) : ((op1) == 0x36))
107
 
108
#define XTENSA_ENTRY_LENGTH     3
109
 
110
/* windowing_enabled() returns true, if windowing is enabled.
111
   WOE must be set to 1; EXCM to 0.
112
   Note: We assume that EXCM is always 0 for XEA1.  */
113
 
114
#define PS_WOE                  (1<<18)
115
#define PS_EXC                  (1<<4)
116
 
117
/* Convert a live Ax register number to the corresponding Areg number.  */
118
static int
119
areg_number (struct gdbarch *gdbarch, int regnum, ULONGEST wb)
120
{
121
  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
122
  int areg;
123
 
124
  areg = regnum - tdep->a0_base;
125
  areg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
126
  areg &= tdep->num_aregs - 1;
127
 
128
  return areg + tdep->ar_base;
129
}
130
 
131
static inline int
132
windowing_enabled (CORE_ADDR ps)
133
{
134
  return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
135
}
136
 
137
/* Return the window size of the previous call to the function from which we
138
   have just returned.
139
 
140
   This function is used to extract the return value after a called function
141
   has returned to the caller.  On Xtensa, the register that holds the return
142
   value (from the perspective of the caller) depends on what call
143
   instruction was used.  For now, we are assuming that the call instruction
144
   precedes the current address, so we simply analyze the call instruction.
145
   If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
146
   method to call the inferior function.  */
147
 
148
static int
149
extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
150
{
151
  int winsize = 4;
152
  int insn;
153
  gdb_byte buf[4];
154
 
155
  DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
156
 
157
  /* Read the previous instruction (should be a call[x]{4|8|12}.  */
158
  read_memory (pc-3, buf, 3);
159
  insn = extract_unsigned_integer (buf, 3);
160
 
161
  /* Decode call instruction:
162
     Little Endian
163
       call{0,4,8,12}   OFFSET || {00,01,10,11} || 0101
164
       callx{0,4,8,12}  OFFSET || 11 || {00,01,10,11} || 0000
165
     Big Endian
166
       call{0,4,8,12}   0101 || {00,01,10,11} || OFFSET
167
       callx{0,4,8,12}  0000 || {00,01,10,11} || 11 || OFFSET.  */
168
 
169
  if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
170
    {
171
      if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
172
        winsize = (insn & 0x30) >> 2;   /* 0, 4, 8, 12.  */
173
    }
174
  else
175
    {
176
      if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
177
        winsize = (insn >> 16) & 0xc;   /* 0, 4, 8, 12.  */
178
    }
179
  return winsize;
180
}
181
 
182
 
183
/* REGISTER INFORMATION */
184
 
185
/* Returns the name of a register.  */
186
static const char *
187
xtensa_register_name (struct gdbarch *gdbarch, int regnum)
188
{
189
  /* Return the name stored in the register map.  */
190
  if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
191
                              + gdbarch_num_pseudo_regs (gdbarch))
192
    return gdbarch_tdep (gdbarch)->regmap[regnum].name;
193
 
194
  internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
195
  return 0;
196
}
197
 
198
static unsigned long
199
xtensa_read_register (int regnum)
200
{
201
  ULONGEST value;
202
 
203
  regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
204
  return (unsigned long) value;
205
}
206
 
207
/* Return the type of a register.  Create a new type, if necessary.  */
208
 
209
static struct ctype_cache
210
{
211
  struct ctype_cache *next;
212
  int size;
213
  struct type *virtual_type;
214
} *type_entries = NULL;
215
 
216
static struct type *
217
xtensa_register_type (struct gdbarch *gdbarch, int regnum)
218
{
219
  /* Return signed integer for ARx and Ax registers.  */
220
  if ((regnum >= gdbarch_tdep (gdbarch)->ar_base
221
      && regnum < gdbarch_tdep (gdbarch)->ar_base
222
                    + gdbarch_tdep (gdbarch)->num_aregs)
223
      || (regnum >= gdbarch_tdep (gdbarch)->a0_base
224
      && regnum < gdbarch_tdep (gdbarch)->a0_base + 16))
225
    return builtin_type_int;
226
 
227
  if (regnum == gdbarch_pc_regnum (gdbarch)
228
      || regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
229
    return lookup_pointer_type (builtin_type_void);
230
 
231
  /* Return the stored type for all other registers.  */
232
  else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
233
                                   + gdbarch_num_pseudo_regs (gdbarch))
234
    {
235
      xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
236
 
237
      /* Set ctype for this register (only the first time).  */
238
 
239
      if (reg->ctype == 0)
240
        {
241
          struct ctype_cache *tp;
242
          int size = reg->byte_size;
243
 
244
          /* We always use the memory representation,
245
             even if the register width is smaller.  */
246
          switch (size)
247
            {
248
            case 1:
249
              reg->ctype = builtin_type_uint8;
250
              break;
251
 
252
            case 2:
253
              reg->ctype = builtin_type_uint16;
254
              break;
255
 
256
            case 4:
257
              reg->ctype = builtin_type_uint32;
258
              break;
259
 
260
            case 8:
261
              reg->ctype = builtin_type_uint64;
262
              break;
263
 
264
            case 16:
265
              reg->ctype = builtin_type_uint128;
266
              break;
267
 
268
            default:
269
              for (tp = type_entries; tp != NULL; tp = tp->next)
270
                if (tp->size == size)
271
                  break;
272
 
273
              if (tp == NULL)
274
                {
275
                  char *name = xmalloc (16);
276
                  tp = xmalloc (sizeof (struct ctype_cache));
277
                  tp->next = type_entries;
278
                  type_entries = tp;
279
                  tp->size = size;
280
 
281
                  sprintf (name, "int%d", size * 8);
282
                  tp->virtual_type = init_type (TYPE_CODE_INT, size,
283
                                                TYPE_FLAG_UNSIGNED, name,
284
                                                NULL);
285
                }
286
 
287
              reg->ctype = tp->virtual_type;
288
            }
289
        }
290
      return reg->ctype;
291
    }
292
 
293
  internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
294
  return 0;
295
}
296
 
297
 
298
/* Return the 'local' register number for stubs, dwarf2, etc.
299
   The debugging information enumerates registers starting from 0 for A0
300
   to n for An.  So, we only have to add the base number for A0.  */
301
 
302
static int
303
xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
304
{
305
  int i;
306
 
307
  if (regnum >= 0 && regnum < 16)
308
    return gdbarch_tdep (gdbarch)->a0_base + regnum;
309
 
310
  for (i = 0;
311
       i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
312
       i++)
313
    if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
314
      return i;
315
 
316
  internal_error (__FILE__, __LINE__,
317
                  _("invalid dwarf/stabs register number %d"), regnum);
318
  return 0;
319
}
320
 
321
 
322
/* Write the bits of a masked register to the various registers.
323
   Only the masked areas of these registers are modified; the other
324
   fields are untouched.  The size of masked registers is always less
325
   than or equal to 32 bits.  */
326
 
327
static void
328
xtensa_register_write_masked (struct regcache *regcache,
329
                              xtensa_register_t *reg, const gdb_byte *buffer)
330
{
331
  unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
332
  const xtensa_mask_t *mask = reg->mask;
333
 
334
  int shift = 0;         /* Shift for next mask (mod 32).  */
335
  int start, size;              /* Start bit and size of current mask.  */
336
 
337
  unsigned int *ptr = value;
338
  unsigned int regval, m, mem = 0;
339
 
340
  int bytesize = reg->byte_size;
341
  int bitsize = bytesize * 8;
342
  int i, r;
343
 
344
  DEBUGTRACE ("xtensa_register_write_masked ()\n");
345
 
346
  /* Copy the masked register to host byte-order.  */
347
  if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
348
    for (i = 0; i < bytesize; i++)
349
      {
350
        mem >>= 8;
351
        mem |= (buffer[bytesize - i - 1] << 24);
352
        if ((i & 3) == 3)
353
          *ptr++ = mem;
354
      }
355
  else
356
    for (i = 0; i < bytesize; i++)
357
      {
358
        mem >>= 8;
359
        mem |= (buffer[i] << 24);
360
        if ((i & 3) == 3)
361
          *ptr++ = mem;
362
      }
363
 
364
  /* We might have to shift the final value:
365
     bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
366
     bytesize & 3 == x -> shift (4-x) * 8.  */
367
 
368
  *ptr = mem >> (((0 - bytesize) & 3) * 8);
369
  ptr = value;
370
  mem = *ptr;
371
 
372
  /* Write the bits to the masked areas of the other registers.  */
373
  for (i = 0; i < mask->count; i++)
374
    {
375
      start = mask->mask[i].bit_start;
376
      size = mask->mask[i].bit_size;
377
      regval = mem >> shift;
378
 
379
      if ((shift += size) > bitsize)
380
        error (_("size of all masks is larger than the register"));
381
 
382
      if (shift >= 32)
383
        {
384
          mem = *(++ptr);
385
          shift -= 32;
386
          bitsize -= 32;
387
 
388
          if (shift > 0)
389
            regval |= mem << (size - shift);
390
        }
391
 
392
      /* Make sure we have a valid register.  */
393
      r = mask->mask[i].reg_num;
394
      if (r >= 0 && size > 0)
395
        {
396
          /* Don't overwrite the unmasked areas.  */
397
          ULONGEST old_val;
398
          regcache_cooked_read_unsigned (regcache, r, &old_val);
399
          m = 0xffffffff >> (32 - size) << start;
400
          regval <<= start;
401
          regval = (regval & m) | (old_val & ~m);
402
          regcache_cooked_write_unsigned (regcache, r, regval);
403
        }
404
    }
405
}
406
 
407
 
408
/* Read a tie state or mapped registers.  Read the masked areas
409
   of the registers and assemble them into a single value.  */
410
 
411
static void
412
xtensa_register_read_masked (struct regcache *regcache,
413
                             xtensa_register_t *reg, gdb_byte *buffer)
414
{
415
  unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
416
  const xtensa_mask_t *mask = reg->mask;
417
 
418
  int shift = 0;
419
  int start, size;
420
 
421
  unsigned int *ptr = value;
422
  unsigned int regval, mem = 0;
423
 
424
  int bytesize = reg->byte_size;
425
  int bitsize = bytesize * 8;
426
  int i;
427
 
428
  DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
429
              reg->name == 0 ? "" : reg->name);
430
 
431
  /* Assemble the register from the masked areas of other registers.  */
432
  for (i = 0; i < mask->count; i++)
433
    {
434
      int r = mask->mask[i].reg_num;
435
      if (r >= 0)
436
        {
437
          ULONGEST val;
438
          regcache_cooked_read_unsigned (regcache, r, &val);
439
          regval = (unsigned int) val;
440
        }
441
      else
442
        regval = 0;
443
 
444
      start = mask->mask[i].bit_start;
445
      size = mask->mask[i].bit_size;
446
 
447
      regval >>= start;
448
 
449
      if (size < 32)
450
        regval &= (0xffffffff >> (32 - size));
451
 
452
      mem |= regval << shift;
453
 
454
      if ((shift += size) > bitsize)
455
        error (_("size of all masks is larger than the register"));
456
 
457
      if (shift >= 32)
458
        {
459
          *ptr++ = mem;
460
          bitsize -= 32;
461
          shift -= 32;
462
 
463
          if (shift == 0)
464
            mem = 0;
465
          else
466
            mem = regval >> (size - shift);
467
        }
468
    }
469
 
470
  if (shift > 0)
471
    *ptr = mem;
472
 
473
  /* Copy value to target byte order.  */
474
  ptr = value;
475
  mem = *ptr;
476
 
477
  if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
478
    for (i = 0; i < bytesize; i++)
479
      {
480
        if ((i & 3) == 0)
481
          mem = *ptr++;
482
        buffer[bytesize - i - 1] = mem & 0xff;
483
        mem >>= 8;
484
      }
485
  else
486
    for (i = 0; i < bytesize; i++)
487
      {
488
        if ((i & 3) == 0)
489
          mem = *ptr++;
490
        buffer[i] = mem & 0xff;
491
        mem >>= 8;
492
      }
493
}
494
 
495
 
496
/* Read pseudo registers.  */
497
 
498
static void
499
xtensa_pseudo_register_read (struct gdbarch *gdbarch,
500
                             struct regcache *regcache,
501
                             int regnum,
502
                             gdb_byte *buffer)
503
{
504
  DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
505
              regnum, xtensa_register_name (gdbarch, regnum));
506
 
507
  if (regnum == gdbarch_num_regs (gdbarch)
508
                + gdbarch_num_pseudo_regs (gdbarch) - 1)
509
     regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
510
 
511
  /* Read aliases a0..a15, if this is a Windowed ABI.  */
512
  if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
513
      && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
514
      && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
515
    {
516
      gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
517
 
518
      regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
519
      regnum = areg_number (gdbarch, regnum, extract_unsigned_integer (buf, 4));
520
    }
521
 
522
  /* We can always read non-pseudo registers.  */
523
  if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
524
    regcache_raw_read (regcache, regnum, buffer);
525
 
526
 
527
  /* We have to find out how to deal with priveleged registers.
528
     Let's treat them as pseudo-registers, but we cannot read/write them.  */
529
 
530
  else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
531
    {
532
      buffer[0] = (gdb_byte)0;
533
      buffer[1] = (gdb_byte)0;
534
      buffer[2] = (gdb_byte)0;
535
      buffer[3] = (gdb_byte)0;
536
    }
537
  /* Pseudo registers.  */
538
  else if (regnum >= 0
539
            && regnum < gdbarch_num_regs (gdbarch)
540
                        + gdbarch_num_pseudo_regs (gdbarch))
541
    {
542
      xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
543
      xtensa_register_type_t type = reg->type;
544
      int flags = gdbarch_tdep (gdbarch)->target_flags;
545
 
546
      /* We cannot read Unknown or Unmapped registers.  */
547
      if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
548
        {
549
          if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
550
            {
551
              warning (_("cannot read register %s"),
552
                       xtensa_register_name (gdbarch, regnum));
553
              return;
554
            }
555
        }
556
 
557
      /* Some targets cannot read TIE register files.  */
558
      else if (type == xtRegisterTypeTieRegfile)
559
        {
560
          /* Use 'fetch' to get register?  */
561
          if (flags & xtTargetFlagsUseFetchStore)
562
            {
563
              warning (_("cannot read register"));
564
              return;
565
            }
566
 
567
          /* On some targets (esp. simulators), we can always read the reg.  */
568
          else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
569
            {
570
              warning (_("cannot read register"));
571
              return;
572
            }
573
        }
574
 
575
      /* We can always read mapped registers.  */
576
      else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
577
        {
578
          xtensa_register_read_masked (regcache, reg, buffer);
579
          return;
580
        }
581
 
582
      /* Assume that we can read the register.  */
583
      regcache_raw_read (regcache, regnum, buffer);
584
    }
585
  else
586
    internal_error (__FILE__, __LINE__,
587
                    _("invalid register number %d"), regnum);
588
}
589
 
590
 
591
/* Write pseudo registers.  */
592
 
593
static void
594
xtensa_pseudo_register_write (struct gdbarch *gdbarch,
595
                              struct regcache *regcache,
596
                              int regnum,
597
                              const gdb_byte *buffer)
598
{
599
  DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
600
              regnum, xtensa_register_name (gdbarch, regnum));
601
 
602
  if (regnum == gdbarch_num_regs (gdbarch)
603
                + gdbarch_num_pseudo_regs (gdbarch) -1)
604
     regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
605
 
606
  /* Renumber register, if aliase a0..a15 on Windowed ABI.  */
607
  if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
608
      && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
609
      && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
610
    {
611
      gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
612
      unsigned int wb;
613
 
614
      regcache_raw_read (regcache,
615
                         gdbarch_tdep (gdbarch)->wb_regnum, buf);
616
      regnum = areg_number (gdbarch, regnum, extract_unsigned_integer (buf, 4));
617
    }
618
 
619
  /* We can always write 'core' registers.
620
     Note: We might have converted Ax->ARy.  */
621
  if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
622
    regcache_raw_write (regcache, regnum, buffer);
623
 
624
  /* We have to find out how to deal with priveleged registers.
625
     Let's treat them as pseudo-registers, but we cannot read/write them.  */
626
 
627
  else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
628
    {
629
      return;
630
    }
631
  /* Pseudo registers.  */
632
  else if (regnum >= 0
633
           && regnum < gdbarch_num_regs (gdbarch)
634
                       + gdbarch_num_pseudo_regs (gdbarch))
635
    {
636
      xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
637
      xtensa_register_type_t type = reg->type;
638
      int flags = gdbarch_tdep (gdbarch)->target_flags;
639
 
640
      /* On most targets, we cannot write registers
641
         of type "Unknown" or "Unmapped".  */
642
      if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
643
        {
644
          if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
645
            {
646
              warning (_("cannot write register %s"),
647
                       xtensa_register_name (gdbarch, regnum));
648
              return;
649
            }
650
        }
651
 
652
      /* Some targets cannot read TIE register files.  */
653
      else if (type == xtRegisterTypeTieRegfile)
654
        {
655
          /* Use 'store' to get register?  */
656
          if (flags & xtTargetFlagsUseFetchStore)
657
            {
658
              warning (_("cannot write register"));
659
              return;
660
            }
661
 
662
          /* On some targets (esp. simulators), we can always write
663
             the register.  */
664
          else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
665
            {
666
              warning (_("cannot write register"));
667
              return;
668
            }
669
        }
670
 
671
      /* We can always write mapped registers.  */
672
      else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
673
        {
674
          xtensa_register_write_masked (regcache, reg, buffer);
675
          return;
676
        }
677
 
678
      /* Assume that we can write the register.  */
679
      regcache_raw_write (regcache, regnum, buffer);
680
    }
681
  else
682
    internal_error (__FILE__, __LINE__,
683
                    _("invalid register number %d"), regnum);
684
}
685
 
686
static struct reggroup *xtensa_ar_reggroup;
687
static struct reggroup *xtensa_user_reggroup;
688
static struct reggroup *xtensa_vectra_reggroup;
689
static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
690
 
691
static void
692
xtensa_init_reggroups (void)
693
{
694
  xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
695
  xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
696
  xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
697
 
698
  xtensa_cp[0] = reggroup_new ("cp0", USER_REGGROUP);
699
  xtensa_cp[1] = reggroup_new ("cp1", USER_REGGROUP);
700
  xtensa_cp[2] = reggroup_new ("cp2", USER_REGGROUP);
701
  xtensa_cp[3] = reggroup_new ("cp3", USER_REGGROUP);
702
  xtensa_cp[4] = reggroup_new ("cp4", USER_REGGROUP);
703
  xtensa_cp[5] = reggroup_new ("cp5", USER_REGGROUP);
704
  xtensa_cp[6] = reggroup_new ("cp6", USER_REGGROUP);
705
  xtensa_cp[7] = reggroup_new ("cp7", USER_REGGROUP);
706
}
707
 
708
static void
709
xtensa_add_reggroups (struct gdbarch *gdbarch)
710
{
711
  int i;
712
 
713
  /* Predefined groups.  */
714
  reggroup_add (gdbarch, all_reggroup);
715
  reggroup_add (gdbarch, save_reggroup);
716
  reggroup_add (gdbarch, restore_reggroup);
717
  reggroup_add (gdbarch, system_reggroup);
718
  reggroup_add (gdbarch, vector_reggroup);
719
  reggroup_add (gdbarch, general_reggroup);
720
  reggroup_add (gdbarch, float_reggroup);
721
 
722
  /* Xtensa-specific groups.  */
723
  reggroup_add (gdbarch, xtensa_ar_reggroup);
724
  reggroup_add (gdbarch, xtensa_user_reggroup);
725
  reggroup_add (gdbarch, xtensa_vectra_reggroup);
726
 
727
  for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
728
    reggroup_add (gdbarch, xtensa_cp[i]);
729
}
730
 
731
static int
732
xtensa_coprocessor_register_group (struct reggroup *group)
733
{
734
  int i;
735
 
736
  for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
737
    if (group == xtensa_cp[i])
738
      return i;
739
 
740
  return -1;
741
}
742
 
743
#define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
744
                        | XTENSA_REGISTER_FLAGS_WRITABLE \
745
                        | XTENSA_REGISTER_FLAGS_VOLATILE)
746
 
747
#define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
748
                        | XTENSA_REGISTER_FLAGS_WRITABLE)
749
 
750
static int
751
xtensa_register_reggroup_p (struct gdbarch *gdbarch,
752
                            int regnum,
753
                            struct reggroup *group)
754
{
755
  xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
756
  xtensa_register_type_t type = reg->type;
757
  xtensa_register_group_t rg = reg->group;
758
  int cp_number;
759
 
760
  /* First, skip registers that are not visible to this target
761
     (unknown and unmapped registers when not using ISS).  */
762
 
763
  if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
764
    return 0;
765
  if (group == all_reggroup)
766
    return 1;
767
  if (group == xtensa_ar_reggroup)
768
    return rg & xtRegisterGroupAddrReg;
769
  if (group == xtensa_user_reggroup)
770
    return rg & xtRegisterGroupUser;
771
  if (group == float_reggroup)
772
    return rg & xtRegisterGroupFloat;
773
  if (group == general_reggroup)
774
    return rg & xtRegisterGroupGeneral;
775
  if (group == float_reggroup)
776
    return rg & xtRegisterGroupFloat;
777
  if (group == system_reggroup)
778
    return rg & xtRegisterGroupState;
779
  if (group == vector_reggroup || group == xtensa_vectra_reggroup)
780
    return rg & xtRegisterGroupVectra;
781
  if (group == save_reggroup || group == restore_reggroup)
782
    return (regnum < gdbarch_num_regs (gdbarch)
783
            && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
784
  if ((cp_number = xtensa_coprocessor_register_group (group)) >= 0)
785
    return rg & (xtRegisterGroupCP0 << cp_number);
786
  else
787
    return 1;
788
}
789
 
790
 
791
/* Supply register REGNUM from the buffer specified by GREGS and LEN
792
   in the general-purpose register set REGSET to register cache
793
   REGCACHE.  If REGNUM is -1 do this for all registers in REGSET.  */
794
 
795
static void
796
xtensa_supply_gregset (const struct regset *regset,
797
                       struct regcache *rc,
798
                       int regnum,
799
                       const void *gregs,
800
                       size_t len)
801
{
802
  const xtensa_elf_gregset_t *regs = gregs;
803
  struct gdbarch *gdbarch = get_regcache_arch (rc);
804
  int i;
805
 
806
  DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...) \n", regnum);
807
 
808
  if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
809
    regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) &regs->pc);
810
  if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
811
    regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) &regs->ps);
812
  if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
813
    regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
814
                         (char *) &regs->windowbase);
815
  if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
816
    regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
817
                         (char *) &regs->windowstart);
818
  if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
819
    regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
820
                         (char *) &regs->lbeg);
821
  if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
822
    regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
823
                         (char *) &regs->lend);
824
  if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
825
    regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
826
                         (char *) &regs->lcount);
827
  if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
828
    regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
829
                         (char *) &regs->sar);
830
  if (regnum >=gdbarch_tdep (gdbarch)->ar_base
831
      && regnum < gdbarch_tdep (gdbarch)->ar_base
832
                    + gdbarch_tdep (gdbarch)->num_aregs)
833
    regcache_raw_supply (rc, regnum,
834
                         (char *) &regs->ar[regnum - gdbarch_tdep
835
                           (gdbarch)->ar_base]);
836
  else if (regnum == -1)
837
    {
838
      for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
839
        regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
840
                             (char *) &regs->ar[i]);
841
    }
842
}
843
 
844
 
845
/* Xtensa register set.  */
846
 
847
static struct regset
848
xtensa_gregset =
849
{
850
  NULL,
851
  xtensa_supply_gregset
852
};
853
 
854
 
855
/* Return the appropriate register set for the core
856
   section identified by SECT_NAME and SECT_SIZE.  */
857
 
858
static const struct regset *
859
xtensa_regset_from_core_section (struct gdbarch *core_arch,
860
                                 const char *sect_name,
861
                                 size_t sect_size)
862
{
863
  DEBUGTRACE ("xtensa_regset_from_core_section "
864
              "(..., sect_name==\"%s\", sect_size==%x) \n",
865
              sect_name, (unsigned int) sect_size);
866
 
867
  if (strcmp (sect_name, ".reg") == 0
868
      && sect_size >= sizeof(xtensa_elf_gregset_t))
869
    return &xtensa_gregset;
870
 
871
  return NULL;
872
}
873
 
874
 
875
/* Handling frames.  */
876
 
877
/* Number of registers to save in case of Windowed ABI.  */
878
#define XTENSA_NUM_SAVED_AREGS          12
879
 
880
/* Frame cache part for Windowed ABI.  */
881
typedef struct xtensa_windowed_frame_cache
882
{
883
  int wb;               /* Base for this frame; -1 if not in regfile.  */
884
  int callsize;         /* Call size to next frame.  */
885
  int ws;
886
  CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
887
} xtensa_windowed_frame_cache_t;
888
 
889
/* Call0 ABI Definitions.  */
890
 
891
#define C0_MAXOPDS  3   /* Maximum number of operands for prologue analysis.  */
892
#define C0_NREGS   16   /* Number of A-registers to track.  */
893
#define C0_CLESV   12   /* Callee-saved registers are here and up.  */
894
#define C0_SP       1   /* Register used as SP.  */
895
#define C0_FP      15   /* Register used as FP.  */
896
#define C0_RA       0   /* Register used as return address.  */
897
#define C0_ARGS     2   /* Register used as first arg/retval.  */
898
#define C0_NARGS    6   /* Number of A-regs for args/retvals.  */
899
 
900
/* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
901
   A-register where the current content of the reg came from (in terms
902
   of an original reg and a constant).  Negative values of c0_rt[n].fp_reg
903
   mean that the orignal content of the register was saved to the stack.
904
   c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
905
   know where SP will end up until the entire prologue has been analyzed.  */
906
 
907
#define C0_CONST   -1   /* fr_reg value if register contains a constant.  */
908
#define C0_INEXP   -2   /* fr_reg value if inexpressible as reg + offset.  */
909
#define C0_NOSTK   -1   /* to_stk value if register has not been stored.  */
910
 
911
extern xtensa_isa xtensa_default_isa;
912
 
913
typedef struct xtensa_c0reg
914
{
915
    int     fr_reg;     /* original register from which register content
916
                           is derived, or C0_CONST, or C0_INEXP.  */
917
    int     fr_ofs;     /* constant offset from reg, or immediate value.  */
918
    int     to_stk;     /* offset from original SP to register (4-byte aligned),
919
                           or C0_NOSTK if register has not been saved.  */
920
} xtensa_c0reg_t;
921
 
922
 
923
/* Frame cache part for Call0 ABI.  */
924
typedef struct xtensa_call0_frame_cache
925
{
926
  int c0_frmsz;                         /* Stack frame size.  */
927
  int c0_hasfp;                         /* Current frame uses frame pointer.  */
928
  int fp_regnum;                        /* A-register used as FP.  */
929
  int c0_fp;                            /* Actual value of frame pointer.  */
930
  xtensa_c0reg_t c0_rt[C0_NREGS];       /* Register tracking information.  */
931
} xtensa_call0_frame_cache_t;
932
 
933
typedef struct xtensa_frame_cache
934
{
935
  CORE_ADDR base;       /* Stack pointer of the next frame.  */
936
  CORE_ADDR pc;         /* PC at the entry point to the function.  */
937
  CORE_ADDR ra;         /* The raw return address.  */
938
  CORE_ADDR ps;         /* The PS register of the frame.  */
939
  CORE_ADDR prev_sp;    /* Stack Pointer of the frame.  */
940
  int call0;            /* It's a call0 framework (else windowed).  */
941
  union
942
    {
943
      xtensa_windowed_frame_cache_t     wd;     /* call0 == false.  */
944
      xtensa_call0_frame_cache_t        c0;     /* call0 == true.  */
945
    };
946
} xtensa_frame_cache_t;
947
 
948
 
949
static struct xtensa_frame_cache *
950
xtensa_alloc_frame_cache (int windowed)
951
{
952
  xtensa_frame_cache_t *cache;
953
  int i;
954
 
955
  DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
956
 
957
  cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
958
 
959
  cache->base = 0;
960
  cache->pc = 0;
961
  cache->ra = 0;
962
  cache->ps = 0;
963
  cache->prev_sp = 0;
964
  cache->call0 = !windowed;
965
  if (cache->call0)
966
    {
967
      cache->c0.c0_frmsz  = -1;
968
      cache->c0.c0_hasfp  =  0;
969
      cache->c0.fp_regnum = -1;
970
      cache->c0.c0_fp     = -1;
971
 
972
      for (i = 0; i < C0_NREGS; i++)
973
        {
974
          cache->c0.c0_rt[i].fr_reg = i;
975
          cache->c0.c0_rt[i].fr_ofs = 0;
976
          cache->c0.c0_rt[i].to_stk = C0_NOSTK;
977
        }
978
    }
979
  else
980
    {
981
      cache->wd.wb = 0;
982
      cache->wd.callsize = -1;
983
 
984
      for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
985
        cache->wd.aregs[i] = -1;
986
    }
987
  return cache;
988
}
989
 
990
 
991
static CORE_ADDR
992
xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
993
{
994
  return address & ~15;
995
}
996
 
997
 
998
static CORE_ADDR
999
xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1000
{
1001
  gdb_byte buf[8];
1002
 
1003
  DEBUGTRACE ("xtensa_unwind_pc (next_frame = %p)\n", next_frame);
1004
 
1005
  frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1006
 
1007
  DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int)
1008
             extract_typed_address (buf, builtin_type_void_func_ptr));
1009
 
1010
  return extract_typed_address (buf, builtin_type_void_func_ptr);
1011
}
1012
 
1013
 
1014
static struct frame_id
1015
xtensa_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1016
{
1017
  CORE_ADDR pc, fp;
1018
 
1019
  /* next_frame->prev is a dummy frame.  Return a frame ID of that frame.  */
1020
 
1021
  DEBUGTRACE ("xtensa_unwind_dummy_id ()\n");
1022
 
1023
  pc = frame_pc_unwind (next_frame);
1024
  fp = frame_unwind_register_unsigned
1025
         (next_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1026
 
1027
  /* Make dummy frame ID unique by adding a constant.  */
1028
  return frame_id_build (fp + SP_ALIGNMENT, pc);
1029
}
1030
 
1031
/* The key values to identify the frame using "cache" are
1032
 
1033
        cache->base    = SP of this frame;
1034
        cache->pc      = entry-PC (entry point of the frame function);
1035
        cache->prev_sp = SP of the previous frame.
1036
*/
1037
 
1038
static void
1039
call0_frame_cache (struct frame_info *next_frame,
1040
                   xtensa_frame_cache_t *cache,
1041
                   CORE_ADDR pc);
1042
 
1043
static struct xtensa_frame_cache *
1044
xtensa_frame_cache (struct frame_info *next_frame, void **this_cache)
1045
{
1046
  xtensa_frame_cache_t *cache;
1047
  CORE_ADDR ra, wb, ws, pc, sp, ps;
1048
  struct gdbarch *gdbarch = get_frame_arch (next_frame);
1049
  unsigned int ps_regnum = gdbarch_ps_regnum (gdbarch);
1050
  char op1;
1051
  int  windowed;
1052
 
1053
  DEBUGTRACE ("xtensa_frame_cache (next_frame %p, *this_cache %p)\n",
1054
              next_frame, this_cache ? *this_cache : (void*)0xdeadbeef);
1055
 
1056
  if (*this_cache)
1057
    return *this_cache;
1058
 
1059
  windowed = windowing_enabled (xtensa_read_register (ps_regnum));
1060
 
1061
  /* Get pristine xtensa-frame.  */
1062
  cache = xtensa_alloc_frame_cache (windowed);
1063
  *this_cache = cache;
1064
 
1065
  pc = frame_unwind_register_unsigned (next_frame,
1066
                                       gdbarch_pc_regnum (gdbarch));
1067
 
1068
  if (windowed)
1069
    {
1070
      /* Get WINDOWBASE, WINDOWSTART, and PS registers.  */
1071
      wb = frame_unwind_register_unsigned
1072
             (next_frame, gdbarch_tdep (gdbarch)->wb_regnum);
1073
      ws = frame_unwind_register_unsigned
1074
             (next_frame, gdbarch_tdep (gdbarch)->ws_regnum);
1075
      ps = frame_unwind_register_unsigned (next_frame, ps_regnum);
1076
 
1077
      op1 = read_memory_integer (pc, 1);
1078
      if (XTENSA_IS_ENTRY (gdbarch, op1))
1079
        {
1080
          int callinc = CALLINC (ps);
1081
          ra = frame_unwind_register_unsigned
1082
            (next_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
1083
 
1084
          DEBUGINFO("[xtensa_frame_cache] 'entry' at 0x%08x\n (callinc = %d)",
1085
                    (int)pc, callinc);
1086
 
1087
          /* ENTRY hasn't been executed yet, therefore callsize is still 0.  */
1088
          cache->wd.callsize = 0;
1089
          cache->wd.wb = wb;
1090
          cache->wd.ws = ws;
1091
          cache->prev_sp = frame_unwind_register_unsigned
1092
                             (next_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1093
        }
1094
      else
1095
        {
1096
          ra = frame_unwind_register_unsigned
1097
                 (next_frame, gdbarch_tdep (gdbarch)->a0_base);
1098
          cache->wd.callsize = WINSIZE (ra);
1099
          cache->wd.wb = (wb - cache->wd.callsize / 4)
1100
                          & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
1101
          cache->wd.ws = ws & ~(1 << wb);
1102
        }
1103
 
1104
      cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
1105
      cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1106
      cache->ps = (ps & ~PS_CALLINC_MASK)
1107
        | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1108
 
1109
      if (cache->wd.ws == 0)
1110
        {
1111
          int i;
1112
 
1113
          /* Set A0...A3.  */
1114
          sp = frame_unwind_register_unsigned
1115
                 (next_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
1116
 
1117
          for (i = 0; i < 4; i++, sp += 4)
1118
            {
1119
              cache->wd.aregs[i] = sp;
1120
            }
1121
 
1122
          if (cache->wd.callsize > 4)
1123
            {
1124
              /* Set A4...A7/A11.  */
1125
              /* Read an SP of the previous frame.  */
1126
              sp = (CORE_ADDR) read_memory_integer (sp - 12, 4);
1127
              sp = (CORE_ADDR) read_memory_integer (sp - 12, 4);
1128
              sp -= cache->wd.callsize * 4;
1129
 
1130
              for ( /* i=4  */ ; i < cache->wd.callsize; i++, sp += 4)
1131
                {
1132
                  cache->wd.aregs[i] = sp;
1133
                }
1134
            }
1135
        }
1136
 
1137
      if ((cache->prev_sp == 0) && ( ra != 0 ))
1138
        /* If RA is equal to 0 this frame is an outermost frame.  Leave
1139
           cache->prev_sp unchanged marking the boundary of the frame stack.  */
1140
        {
1141
          if (cache->wd.ws == 0)
1142
            {
1143
              /* Register window overflow already happened.
1144
                 We can read caller's SP from the proper spill loction.  */
1145
              cache->prev_sp =
1146
                read_memory_integer (cache->wd.aregs[1],
1147
                                     register_size (gdbarch,
1148
                                       gdbarch_tdep (gdbarch)->a0_base + 1));
1149
            }
1150
          else
1151
            {
1152
              /* Read caller's frame SP directly from the previous window.  */
1153
              int regnum = areg_number
1154
                             (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
1155
                              cache->wd.wb);
1156
 
1157
              cache->prev_sp = xtensa_read_register (regnum);
1158
            }
1159
        }
1160
    }
1161
  else  /* Call0 framework.  */
1162
    {
1163
      call0_frame_cache (next_frame, cache, pc);
1164
    }
1165
 
1166
  cache->base = frame_unwind_register_unsigned
1167
                  (next_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1168
 
1169
  return cache;
1170
}
1171
 
1172
static void
1173
xtensa_frame_this_id (struct frame_info *next_frame,
1174
                      void **this_cache,
1175
                      struct frame_id *this_id)
1176
{
1177
  struct xtensa_frame_cache *cache =
1178
    xtensa_frame_cache (next_frame, this_cache);
1179
  struct frame_id id;
1180
 
1181
  DEBUGTRACE ("xtensa_frame_this_id (next 0x%lx, *this 0x%lx)\n",
1182
              (unsigned long) next_frame, (unsigned long) *this_cache);
1183
 
1184
  if (cache->prev_sp == 0)
1185
    return;
1186
 
1187
  id = frame_id_build (cache->prev_sp, cache->pc);
1188
  if (frame_id_eq (id, get_frame_id(next_frame)))
1189
    {
1190
      warning(_("\
1191
Frame stack is corrupted. That could happen because of \
1192
setting register(s) from GDB or stopping execution \
1193
inside exception handler. Frame backtracing has stopped. \
1194
It can make some GDB commands work inappropriately.\n"));
1195
      cache->prev_sp = 0;
1196
      return;
1197
    }
1198
  (*this_id) = id;
1199
}
1200
 
1201
static int
1202
call0_frame_get_reg_at_entry (struct frame_info *next_frame,
1203
                              struct xtensa_frame_cache *cache,
1204
                              int regnum,
1205
                              CORE_ADDR *addrp,
1206
                              enum lval_type *lval,
1207
                              gdb_byte *valuep)
1208
{
1209
  CORE_ADDR fp, spe;
1210
  int stkofs;
1211
  struct gdbarch *gdbarch = get_frame_arch (next_frame);
1212
  int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
1213
            && regnum <= (gdbarch_tdep (gdbarch)->ar_base + C0_NREGS))
1214
              ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
1215
 
1216
  /* Determine stack pointer on entry to this function, based on FP.  */
1217
  spe = cache->c0.c0_fp - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1218
 
1219
  /* If register was saved to the stack frame in the prologue, retrieve it.  */
1220
  stkofs = cache->c0.c0_rt[reg].to_stk;
1221
  if (stkofs != C0_NOSTK)
1222
    {
1223
      *lval = lval_memory;
1224
      *addrp = spe + stkofs;
1225
 
1226
      if (valuep)
1227
        read_memory (*addrp, valuep, register_size (gdbarch, regnum));
1228
 
1229
      return 1;
1230
    }
1231
 
1232
  /* If not callee-saved or if known to have been overwritten, give up.  */
1233
  if (reg < C0_CLESV
1234
      || cache->c0.c0_rt[reg].fr_reg != reg
1235
      || cache->c0.c0_rt[reg].fr_ofs != 0)
1236
    return 0;
1237
 
1238
  if (get_frame_type (next_frame) != NORMAL_FRAME)
1239
    /* TODO: Do we need a special case for DUMMY_FRAME here?  */
1240
    return 0;
1241
 
1242
  return call0_frame_get_reg_at_entry (get_next_frame(next_frame),
1243
                                       cache, regnum, addrp, lval, valuep);
1244
}
1245
 
1246
static void
1247
xtensa_frame_prev_register (struct frame_info *next_frame,
1248
                            void **this_cache,
1249
                            int regnum,
1250
                            int *optimizedp,
1251
                            enum lval_type *lvalp,
1252
                            CORE_ADDR *addrp,
1253
                            int *realnump,
1254
                            gdb_byte *valuep)
1255
{
1256
  struct gdbarch *gdbarch = get_frame_arch (next_frame);
1257
  struct xtensa_frame_cache *cache =
1258
    xtensa_frame_cache (next_frame, this_cache);
1259
  CORE_ADDR saved_reg = 0;
1260
  int done = 1;
1261
 
1262
  DEBUGTRACE ("xtensa_frame_prev_register (next 0x%lx, "
1263
              "*this 0x%lx, regnum %d (%s), ...)\n",
1264
              (unsigned long) next_frame,
1265
              *this_cache ? (unsigned long) *this_cache : 0, regnum,
1266
              xtensa_register_name (gdbarch, regnum));
1267
 
1268
  if (regnum ==gdbarch_pc_regnum (gdbarch))
1269
    saved_reg = cache->ra;
1270
  else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
1271
    saved_reg = cache->prev_sp;
1272
  else if (!cache->call0)
1273
    {
1274
      if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
1275
        {
1276
          if (cache->wd.ws != 0)
1277
            saved_reg = cache->wd.ws;
1278
          else
1279
            saved_reg = 1 << cache->wd.wb;
1280
        }
1281
      else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
1282
        saved_reg = cache->wd.wb;
1283
      else if (regnum == gdbarch_ps_regnum (gdbarch))
1284
        saved_reg = cache->ps;
1285
      else
1286
        done = 0;
1287
    }
1288
  else
1289
    done = 0;
1290
 
1291
  if (done)
1292
    {
1293
      *optimizedp = 0;
1294
      *lvalp = not_lval;
1295
      *addrp = 0;
1296
      *realnump = -1;
1297
      if (valuep)
1298
        store_unsigned_integer (valuep, 4, saved_reg);
1299
 
1300
      return;
1301
    }
1302
 
1303
  if (!cache->call0) /* Windowed ABI.  */
1304
    {
1305
      /* Convert A-register numbers to AR-register numbers.  */
1306
      if (regnum >= gdbarch_tdep (gdbarch)->a0_base
1307
          && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
1308
        regnum = areg_number (gdbarch, regnum, cache->wd.wb);
1309
 
1310
      /* Check if AR-register has been saved to stack.  */
1311
      if (regnum >= gdbarch_tdep (gdbarch)->ar_base
1312
          && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1313
                         + gdbarch_tdep (gdbarch)->num_aregs))
1314
        {
1315
          int areg = regnum - gdbarch_tdep (gdbarch)->ar_base
1316
                       - (cache->wd.wb * 4);
1317
 
1318
          if (areg >= 0
1319
              && areg < XTENSA_NUM_SAVED_AREGS
1320
              && cache->wd.aregs[areg] != -1)
1321
            {
1322
              *optimizedp = 0;
1323
              *lvalp = lval_memory;
1324
              *addrp = cache->wd.aregs[areg];
1325
              *realnump = -1;
1326
 
1327
              if (valuep)
1328
                read_memory (*addrp, valuep,
1329
                             register_size (gdbarch, regnum));
1330
 
1331
              DEBUGINFO ("[xtensa_frame_prev_register] register on stack\n");
1332
              return;
1333
            }
1334
        }
1335
    }
1336
  else /* Call0 ABI.  */
1337
    {
1338
      int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
1339
                && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1340
                               + C0_NREGS))
1341
                  ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
1342
 
1343
      if (reg < C0_NREGS)
1344
        {
1345
          CORE_ADDR spe;
1346
          int stkofs;
1347
 
1348
          /* If register was saved in the prologue, retrieve it.  */
1349
          stkofs = cache->c0.c0_rt[reg].to_stk;
1350
          if (stkofs != C0_NOSTK)
1351
            {
1352
              /* Determine SP on entry based on FP.  */
1353
              spe = cache->c0.c0_fp
1354
                - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1355
              *optimizedp = 0;
1356
              *lvalp = lval_memory;
1357
              *addrp = spe + stkofs;
1358
              *realnump = -1;
1359
 
1360
              if (valuep)
1361
                read_memory (*addrp, valuep,
1362
                             register_size (gdbarch, regnum));
1363
 
1364
              DEBUGINFO ("[xtensa_frame_prev_register] register on stack\n");
1365
              return;
1366
            }
1367
        }
1368
    }
1369
 
1370
  /* All other registers have been either saved to
1371
     the stack or are still alive in the processor.  */
1372
 
1373
  *optimizedp = 0;
1374
  *lvalp = lval_register;
1375
  *addrp = 0;
1376
  *realnump = regnum;
1377
  if (valuep)
1378
    frame_unwind_register (next_frame, (*realnump), valuep);
1379
}
1380
 
1381
 
1382
static const struct frame_unwind
1383
xtensa_frame_unwind =
1384
{
1385
  NORMAL_FRAME,
1386
  xtensa_frame_this_id,
1387
  xtensa_frame_prev_register
1388
};
1389
 
1390
static const struct frame_unwind *
1391
xtensa_frame_sniffer (struct frame_info *next_frame)
1392
{
1393
  return &xtensa_frame_unwind;
1394
}
1395
 
1396
static CORE_ADDR
1397
xtensa_frame_base_address (struct frame_info *next_frame, void **this_cache)
1398
{
1399
  struct xtensa_frame_cache *cache =
1400
    xtensa_frame_cache (next_frame, this_cache);
1401
 
1402
  return cache->base;
1403
}
1404
 
1405
static const struct frame_base
1406
xtensa_frame_base =
1407
{
1408
  &xtensa_frame_unwind,
1409
  xtensa_frame_base_address,
1410
  xtensa_frame_base_address,
1411
  xtensa_frame_base_address
1412
};
1413
 
1414
 
1415
static void
1416
xtensa_extract_return_value (struct type *type,
1417
                             struct regcache *regcache,
1418
                             void *dst)
1419
{
1420
  struct gdbarch *gdbarch = get_regcache_arch (regcache);
1421
  bfd_byte *valbuf = dst;
1422
  int len = TYPE_LENGTH (type);
1423
  ULONGEST pc, wb;
1424
  int callsize, areg;
1425
  int offset = 0;
1426
 
1427
  DEBUGTRACE ("xtensa_extract_return_value (...)\n");
1428
 
1429
  gdb_assert(len > 0);
1430
 
1431
  if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1432
    {
1433
      /* First, we have to find the caller window in the register file.  */
1434
      regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1435
      callsize = extract_call_winsize (gdbarch, pc);
1436
 
1437
      /* On Xtensa, we can return up to 4 words (or 2 for call12).  */
1438
      if (len > (callsize > 8 ? 8 : 16))
1439
        internal_error (__FILE__, __LINE__,
1440
                        _("cannot extract return value of %d bytes long"), len);
1441
 
1442
      /* Get the register offset of the return
1443
         register (A2) in the caller window.  */
1444
      regcache_raw_read_unsigned
1445
        (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1446
      areg = areg_number (gdbarch,
1447
                          gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1448
    }
1449
  else
1450
    {
1451
      /* No windowing hardware - Call0 ABI.  */
1452
      areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1453
    }
1454
 
1455
  DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
1456
 
1457
  if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1458
    offset = 4 - len;
1459
 
1460
  for (; len > 0; len -= 4, areg++, valbuf += 4)
1461
    {
1462
      if (len < 4)
1463
        regcache_raw_read_part (regcache, areg, offset, len, valbuf);
1464
      else
1465
        regcache_raw_read (regcache, areg, valbuf);
1466
    }
1467
}
1468
 
1469
 
1470
static void
1471
xtensa_store_return_value (struct type *type,
1472
                           struct regcache *regcache,
1473
                           const void *dst)
1474
{
1475
  struct gdbarch *gdbarch = get_regcache_arch (regcache);
1476
  const bfd_byte *valbuf = dst;
1477
  unsigned int areg;
1478
  ULONGEST pc, wb;
1479
  int callsize;
1480
  int len = TYPE_LENGTH (type);
1481
  int offset = 0;
1482
 
1483
  DEBUGTRACE ("xtensa_store_return_value (...)\n");
1484
 
1485
  if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1486
    {
1487
      regcache_raw_read_unsigned
1488
        (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1489
      regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1490
      callsize = extract_call_winsize (gdbarch, pc);
1491
 
1492
      if (len > (callsize > 8 ? 8 : 16))
1493
        internal_error (__FILE__, __LINE__,
1494
                        _("unimplemented for this length: %d"),
1495
                        TYPE_LENGTH (type));
1496
      areg = areg_number (gdbarch,
1497
                          gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1498
 
1499
      DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
1500
              callsize, (int) wb);
1501
    }
1502
  else
1503
    {
1504
      areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1505
    }
1506
 
1507
  if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1508
    offset = 4 - len;
1509
 
1510
  for (; len > 0; len -= 4, areg++, valbuf += 4)
1511
    {
1512
      if (len < 4)
1513
        regcache_raw_write_part (regcache, areg, offset, len, valbuf);
1514
      else
1515
        regcache_raw_write (regcache, areg, valbuf);
1516
    }
1517
}
1518
 
1519
 
1520
static enum return_value_convention
1521
xtensa_return_value (struct gdbarch *gdbarch,
1522
                     struct type *valtype,
1523
                     struct regcache *regcache,
1524
                     gdb_byte *readbuf,
1525
                     const gdb_byte *writebuf)
1526
{
1527
  /* Structures up to 16 bytes are returned in registers.  */
1528
 
1529
  int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1530
                        || TYPE_CODE (valtype) == TYPE_CODE_UNION
1531
                        || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1532
                       && TYPE_LENGTH (valtype) > 16);
1533
 
1534
  if (struct_return)
1535
    return RETURN_VALUE_STRUCT_CONVENTION;
1536
 
1537
  DEBUGTRACE ("xtensa_return_value(...)\n");
1538
 
1539
  if (writebuf != NULL)
1540
    {
1541
      xtensa_store_return_value (valtype, regcache, writebuf);
1542
    }
1543
 
1544
  if (readbuf != NULL)
1545
    {
1546
      gdb_assert (!struct_return);
1547
      xtensa_extract_return_value (valtype, regcache, readbuf);
1548
    }
1549
  return RETURN_VALUE_REGISTER_CONVENTION;
1550
}
1551
 
1552
 
1553
/* DUMMY FRAME */
1554
 
1555
static CORE_ADDR
1556
xtensa_push_dummy_call (struct gdbarch *gdbarch,
1557
                        struct value *function,
1558
                        struct regcache *regcache,
1559
                        CORE_ADDR bp_addr,
1560
                        int nargs,
1561
                        struct value **args,
1562
                        CORE_ADDR sp,
1563
                        int struct_return,
1564
                        CORE_ADDR struct_addr)
1565
{
1566
  int i;
1567
  int size, onstack_size;
1568
  gdb_byte *buf = (gdb_byte *) alloca (16);
1569
  CORE_ADDR ra, ps;
1570
  struct argument_info
1571
  {
1572
    const bfd_byte *contents;
1573
    int length;
1574
    int onstack;                /* onstack == 0 => in reg */
1575
    int align;                  /* alignment */
1576
    union
1577
    {
1578
      int offset;               /* stack offset if on stack */
1579
      int regno;                /* regno if in register */
1580
    } u;
1581
  };
1582
 
1583
  struct argument_info *arg_info =
1584
    (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
1585
 
1586
  CORE_ADDR osp = sp;
1587
 
1588
  DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
1589
 
1590
  if (xtensa_debug_level > 3)
1591
    {
1592
      int i;
1593
      DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
1594
      DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
1595
                 "struct_addr=0x%x\n",
1596
                 (int) sp, (int) struct_return, (int) struct_addr);
1597
 
1598
      for (i = 0; i < nargs; i++)
1599
        {
1600
          struct value *arg = args[i];
1601
          struct type *arg_type = check_typedef (value_type (arg));
1602
          fprintf_unfiltered (gdb_stdlog, "%2d: 0x%lx %3d ",
1603
                              i, (unsigned long) arg, TYPE_LENGTH (arg_type));
1604
          switch (TYPE_CODE (arg_type))
1605
            {
1606
            case TYPE_CODE_INT:
1607
              fprintf_unfiltered (gdb_stdlog, "int");
1608
              break;
1609
            case TYPE_CODE_STRUCT:
1610
              fprintf_unfiltered (gdb_stdlog, "struct");
1611
              break;
1612
            default:
1613
              fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
1614
              break;
1615
            }
1616
          fprintf_unfiltered (gdb_stdlog, " 0x%lx\n",
1617
                              (unsigned long) value_contents (arg));
1618
        }
1619
    }
1620
 
1621
  /* First loop: collect information.
1622
     Cast into type_long.  (This shouldn't happen often for C because
1623
     GDB already does this earlier.)  It's possible that GDB could
1624
     do it all the time but it's harmless to leave this code here.  */
1625
 
1626
  size = 0;
1627
  onstack_size = 0;
1628
  i = 0;
1629
 
1630
  if (struct_return)
1631
    size = REGISTER_SIZE;
1632
 
1633
  for (i = 0; i < nargs; i++)
1634
    {
1635
      struct argument_info *info = &arg_info[i];
1636
      struct value *arg = args[i];
1637
      struct type *arg_type = check_typedef (value_type (arg));
1638
 
1639
      switch (TYPE_CODE (arg_type))
1640
        {
1641
        case TYPE_CODE_INT:
1642
        case TYPE_CODE_BOOL:
1643
        case TYPE_CODE_CHAR:
1644
        case TYPE_CODE_RANGE:
1645
        case TYPE_CODE_ENUM:
1646
 
1647
          /* Cast argument to long if necessary as the mask does it too.  */
1648
          if (TYPE_LENGTH (arg_type) < TYPE_LENGTH (builtin_type_long))
1649
            {
1650
              arg_type = builtin_type_long;
1651
              arg = value_cast (arg_type, arg);
1652
            }
1653
          /* Aligment is equal to the type length for the basic types.  */
1654
          info->align = TYPE_LENGTH (arg_type);
1655
          break;
1656
 
1657
        case TYPE_CODE_FLT:
1658
 
1659
          /* Align doubles correctly.  */
1660
          if (TYPE_LENGTH (arg_type) == TYPE_LENGTH (builtin_type_double))
1661
            info->align = TYPE_LENGTH (builtin_type_double);
1662
          else
1663
            info->align = TYPE_LENGTH (builtin_type_long);
1664
          break;
1665
 
1666
        case TYPE_CODE_STRUCT:
1667
        default:
1668
          info->align = TYPE_LENGTH (builtin_type_long);
1669
          break;
1670
        }
1671
      info->length = TYPE_LENGTH (arg_type);
1672
      info->contents = value_contents (arg);
1673
 
1674
      /* Align size and onstack_size.  */
1675
      size = (size + info->align - 1) & ~(info->align - 1);
1676
      onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
1677
 
1678
      if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
1679
        {
1680
          info->onstack = 1;
1681
          info->u.offset = onstack_size;
1682
          onstack_size += info->length;
1683
        }
1684
      else
1685
        {
1686
          info->onstack = 0;
1687
          info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
1688
        }
1689
      size += info->length;
1690
    }
1691
 
1692
  /* Adjust the stack pointer and align it.  */
1693
  sp = align_down (sp - onstack_size, SP_ALIGNMENT);
1694
 
1695
  /* Simulate MOVSP, if Windowed ABI.  */
1696
  if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1697
      && (sp != osp))
1698
    {
1699
      read_memory (osp - 16, buf, 16);
1700
      write_memory (sp - 16, buf, 16);
1701
    }
1702
 
1703
  /* Second Loop: Load arguments.  */
1704
 
1705
  if (struct_return)
1706
    {
1707
      store_unsigned_integer (buf, REGISTER_SIZE, struct_addr);
1708
      regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
1709
    }
1710
 
1711
  for (i = 0; i < nargs; i++)
1712
    {
1713
      struct argument_info *info = &arg_info[i];
1714
 
1715
      if (info->onstack)
1716
        {
1717
          int n = info->length;
1718
          CORE_ADDR offset = sp + info->u.offset;
1719
 
1720
          /* Odd-sized structs are aligned to the lower side of a memory
1721
             word in big-endian mode and require a shift.  This only
1722
             applies for structures smaller than one word.  */
1723
 
1724
          if (n < REGISTER_SIZE
1725
              && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1726
            offset += (REGISTER_SIZE - n);
1727
 
1728
          write_memory (offset, info->contents, info->length);
1729
 
1730
        }
1731
      else
1732
        {
1733
          int n = info->length;
1734
          const bfd_byte *cp = info->contents;
1735
          int r = info->u.regno;
1736
 
1737
          /* Odd-sized structs are aligned to the lower side of registers in
1738
             big-endian mode and require a shift.  The odd-sized leftover will
1739
             be at the end.  Note that this is only true for structures smaller
1740
             than REGISTER_SIZE; for larger odd-sized structures the excess
1741
             will be left-aligned in the register on both endiannesses.  */
1742
 
1743
          if (n < REGISTER_SIZE
1744
              && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1745
            {
1746
              ULONGEST v = extract_unsigned_integer (cp, REGISTER_SIZE);
1747
              v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
1748
 
1749
              store_unsigned_integer (buf, REGISTER_SIZE, v);
1750
              regcache_cooked_write (regcache, r, buf);
1751
 
1752
              cp += REGISTER_SIZE;
1753
              n -= REGISTER_SIZE;
1754
              r++;
1755
            }
1756
          else
1757
            while (n > 0)
1758
              {
1759
                regcache_cooked_write (regcache, r, cp);
1760
 
1761
                cp += REGISTER_SIZE;
1762
                n -= REGISTER_SIZE;
1763
                r++;
1764
              }
1765
        }
1766
    }
1767
 
1768
  /* Set the return address of dummy frame to the dummy address.
1769
     The return address for the current function (in A0) is
1770
     saved in the dummy frame, so we can savely overwrite A0 here.  */
1771
 
1772
  if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1773
    {
1774
      ra = (bp_addr & 0x3fffffff) | 0x40000000;
1775
      regcache_raw_read (regcache, gdbarch_ps_regnum (gdbarch), buf);
1776
      ps = extract_unsigned_integer (buf, 4) & ~0x00030000;
1777
      regcache_cooked_write_unsigned
1778
        (regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
1779
      regcache_cooked_write_unsigned (regcache,
1780
                                      gdbarch_ps_regnum (gdbarch),
1781
                                      ps | 0x00010000);
1782
 
1783
      /* All the registers have been saved.  After executing
1784
         dummy call, they all will be restored.  So it's safe
1785
         to modify WINDOWSTART register to make it look like there
1786
         is only one register window corresponding to WINDOWEBASE.  */
1787
 
1788
      regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
1789
      regcache_cooked_write_unsigned (regcache,
1790
                                      gdbarch_tdep (gdbarch)->ws_regnum,
1791
                                      1 << extract_unsigned_integer (buf, 4));
1792
    }
1793
  else
1794
    {
1795
      /* Simulate CALL0: write RA into A0 register.  */
1796
      regcache_cooked_write_unsigned
1797
        (regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
1798
    }
1799
 
1800
  /* Set new stack pointer and return it.  */
1801
  regcache_cooked_write_unsigned (regcache,
1802
                                  gdbarch_tdep (gdbarch)->a0_base + 1, sp);
1803
  /* Make dummy frame ID unique by adding a constant.  */
1804
  return sp + SP_ALIGNMENT;
1805
}
1806
 
1807
 
1808
/* Return a breakpoint for the current location of PC.  We always use
1809
   the density version if we have density instructions (regardless of the
1810
   current instruction at PC), and use regular instructions otherwise.  */
1811
 
1812
#define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
1813
#define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
1814
#define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
1815
#define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
1816
 
1817
static const unsigned char *
1818
xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1819
                           int *lenptr)
1820
{
1821
  static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
1822
  static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
1823
  static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
1824
  static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;
1825
 
1826
  DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
1827
 
1828
  if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
1829
    {
1830
      if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1831
        {
1832
          *lenptr = sizeof (density_big_breakpoint);
1833
          return density_big_breakpoint;
1834
        }
1835
      else
1836
        {
1837
          *lenptr = sizeof (density_little_breakpoint);
1838
          return density_little_breakpoint;
1839
        }
1840
    }
1841
  else
1842
    {
1843
      if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1844
        {
1845
          *lenptr = sizeof (big_breakpoint);
1846
          return big_breakpoint;
1847
        }
1848
      else
1849
        {
1850
          *lenptr = sizeof (little_breakpoint);
1851
          return little_breakpoint;
1852
        }
1853
    }
1854
}
1855
 
1856
/* Call0 ABI support routines.  */
1857
 
1858
/* Call0 opcode class.  Opcodes are preclassified according to what they
1859
   mean for Call0 prologue analysis, and their number of significant operands.
1860
   The purpose of this is to simplify prologue analysis by separating
1861
   instruction decoding (libisa) from the semantics of prologue analysis.  */
1862
 
1863
typedef enum {
1864
  c0opc_illegal,       /* Unknown to libisa (invalid) or 'ill' opcode.  */
1865
  c0opc_uninteresting, /* Not interesting for Call0 prologue analysis.  */
1866
  c0opc_flow,          /* Flow control insn.  */
1867
  c0opc_entry,         /* ENTRY indicates non-Call0 prologue.  */
1868
  c0opc_break,         /* Debugger software breakpoints.  */
1869
  c0opc_add,           /* Adding two registers.  */
1870
  c0opc_addi,          /* Adding a register and an immediate.  */
1871
  c0opc_sub,           /* Subtracting a register from a register.  */
1872
  c0opc_mov,           /* Moving a register to a register.  */
1873
  c0opc_movi,          /* Moving an immediate to a register.  */
1874
  c0opc_l32r,          /* Loading a literal.  */
1875
  c0opc_s32i,          /* Storing word at fixed offset from a base register.  */
1876
  c0opc_NrOf           /* Number of opcode classifications.  */
1877
} xtensa_insn_kind;
1878
 
1879
 
1880
/* Classify an opcode based on what it means for Call0 prologue analysis.  */
1881
 
1882
static xtensa_insn_kind
1883
call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
1884
{
1885
  const char *opcname;
1886
  xtensa_insn_kind opclass = c0opc_uninteresting;
1887
 
1888
  DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
1889
 
1890
  /* Get opcode name and handle special classifications.  */
1891
 
1892
  opcname = xtensa_opcode_name (isa, opc);
1893
 
1894
  if (opcname == NULL
1895
      || strcasecmp (opcname, "ill") == 0
1896
      || strcasecmp (opcname, "ill.n") == 0)
1897
    opclass = c0opc_illegal;
1898
  else if (strcasecmp (opcname, "break") == 0
1899
           || strcasecmp (opcname, "break.n") == 0)
1900
     opclass = c0opc_break;
1901
  else if (strcasecmp (opcname, "entry") == 0)
1902
    opclass = c0opc_entry;
1903
  else if (xtensa_opcode_is_branch (isa, opc) > 0
1904
           || xtensa_opcode_is_jump   (isa, opc) > 0
1905
           || xtensa_opcode_is_loop   (isa, opc) > 0
1906
           || xtensa_opcode_is_call   (isa, opc) > 0
1907
           || strcasecmp (opcname, "simcall") == 0
1908
           || strcasecmp (opcname, "syscall") == 0)
1909
    opclass = c0opc_flow;
1910
 
1911
  /* Also, classify specific opcodes that need to be tracked.  */
1912
  else if (strcasecmp (opcname, "add") == 0
1913
           || strcasecmp (opcname, "add.n") == 0)
1914
    opclass = c0opc_add;
1915
  else if (strcasecmp (opcname, "addi") == 0
1916
           || strcasecmp (opcname, "addi.n") == 0
1917
           || strcasecmp (opcname, "addmi") == 0)
1918
    opclass = c0opc_addi;
1919
  else if (strcasecmp (opcname, "sub") == 0)
1920
    opclass = c0opc_sub;
1921
  else if (strcasecmp (opcname, "mov.n") == 0
1922
           || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro.  */
1923
    opclass = c0opc_mov;
1924
  else if (strcasecmp (opcname, "movi") == 0
1925
           || strcasecmp (opcname, "movi.n") == 0)
1926
    opclass = c0opc_movi;
1927
  else if (strcasecmp (opcname, "l32r") == 0)
1928
    opclass = c0opc_l32r;
1929
  else if (strcasecmp (opcname, "s32i") == 0
1930
           || strcasecmp (opcname, "s32i.n") == 0)
1931
    opclass = c0opc_s32i;
1932
 
1933
  return opclass;
1934
}
1935
 
1936
/* Tracks register movement/mutation for a given operation, which may
1937
   be within a bundle.  Updates the destination register tracking info
1938
   accordingly.  The pc is needed only for pc-relative load instructions
1939
   (eg. l32r).  The SP register number is needed to identify stores to
1940
   the stack frame.  */
1941
 
1942
static void
1943
call0_track_op (xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
1944
                xtensa_insn_kind opclass, int nods, unsigned odv[],
1945
                CORE_ADDR pc, int spreg)
1946
{
1947
  unsigned litbase, litaddr, litval;
1948
 
1949
  switch (opclass)
1950
    {
1951
    case c0opc_addi:
1952
      /* 3 operands: dst, src, imm.  */
1953
      gdb_assert (nods == 3);
1954
      dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1955
      dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
1956
      break;
1957
    case c0opc_add:
1958
      /* 3 operands: dst, src1, src2.  */
1959
      gdb_assert (nods == 3);
1960
      if      (src[odv[1]].fr_reg == C0_CONST)
1961
        {
1962
          dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
1963
          dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
1964
        }
1965
      else if (src[odv[2]].fr_reg == C0_CONST)
1966
        {
1967
          dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1968
          dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
1969
        }
1970
      else dst[odv[0]].fr_reg = C0_INEXP;
1971
      break;
1972
    case c0opc_sub:
1973
      /* 3 operands: dst, src1, src2.  */
1974
      gdb_assert (nods == 3);
1975
      if      (src[odv[2]].fr_reg == C0_CONST)
1976
        {
1977
          dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1978
          dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
1979
        }
1980
      else dst[odv[0]].fr_reg = C0_INEXP;
1981
      break;
1982
    case c0opc_mov:
1983
      /* 2 operands: dst, src [, src].  */
1984
      gdb_assert (nods == 2);
1985
      dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1986
      dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
1987
      break;
1988
    case c0opc_movi:
1989
      /* 2 operands: dst, imm.  */
1990
      gdb_assert (nods == 2);
1991
      dst[odv[0]].fr_reg = C0_CONST;
1992
      dst[odv[0]].fr_ofs = odv[1];
1993
      break;
1994
    case c0opc_l32r:
1995
      /* 2 operands: dst, literal offset.  */
1996
      gdb_assert (nods == 2);
1997
      /* litbase = xtensa_get_litbase (pc); can be also used.  */
1998
      litbase = (gdbarch_tdep (current_gdbarch)->litbase_regnum == -1)
1999
        ? 0 : xtensa_read_register
2000
                (gdbarch_tdep (current_gdbarch)->litbase_regnum);
2001
      litaddr = litbase & 1
2002
                  ? (litbase & ~1) + (signed)odv[1]
2003
                  : (pc + 3  + (signed)odv[1]) & ~3;
2004
      litval = read_memory_integer(litaddr, 4);
2005
      dst[odv[0]].fr_reg = C0_CONST;
2006
      dst[odv[0]].fr_ofs = litval;
2007
      break;
2008
    case c0opc_s32i:
2009
      /* 3 operands: value, base, offset.  */
2010
      gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
2011
      if (src[odv[1]].fr_reg == spreg        /* Store to stack frame.  */
2012
          && (src[odv[1]].fr_ofs & 3) == 0   /* Alignment preserved.  */
2013
          &&  src[odv[0]].fr_reg >= 0          /* Value is from a register.  */
2014
          &&  src[odv[0]].fr_ofs == 0          /* Value hasn't been modified.  */
2015
          &&  src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time.  */
2016
        {
2017
          /* ISA encoding guarantees alignment.  But, check it anyway.  */
2018
          gdb_assert ((odv[2] & 3) == 0);
2019
          dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
2020
        }
2021
      break;
2022
    default:
2023
        gdb_assert (0);
2024
    }
2025
}
2026
 
2027
/* Analyze prologue of the function at start address to determine if it uses
2028
   the Call0 ABI, and if so track register moves and linear modifications
2029
   in the prologue up to the PC or just beyond the prologue, whichever is first.
2030
   An 'entry' instruction indicates non-Call0 ABI and the end of the prologue.
2031
   The prologue may overlap non-prologue instructions but is guaranteed to end
2032
   by the first flow-control instruction (jump, branch, call or return).
2033
   Since an optimized function may move information around and change the
2034
   stack frame arbitrarily during the prologue, the information is guaranteed
2035
   valid only at the point in the function indicated by the PC.
2036
   May be used to skip the prologue or identify the ABI, w/o tracking.
2037
 
2038
   Returns:   Address of first instruction after prologue, or PC (whichever
2039
              is first), or 0, if decoding failed (in libisa).
2040
   Input args:
2041
      start   Start address of function/prologue.
2042
      pc      Program counter to stop at.  Use 0 to continue to end of prologue.
2043
              If 0, avoids infinite run-on in corrupt code memory by bounding
2044
              the scan to the end of the function if that can be determined.
2045
      nregs   Number of general registers to track (size of rt[] array).
2046
   InOut args:
2047
      rt[]    Array[nregs] of xtensa_c0reg structures for register tracking info.
2048
              If NULL, registers are not tracked.
2049
   Output args:
2050
      call0   If != NULL, *call0 is set non-zero if Call0 ABI used, else 0
2051
              (more accurately, non-zero until 'entry' insn is encountered).
2052
 
2053
      Note that these may produce useful results even if decoding fails
2054
      because they begin with default assumptions that analysis may change.  */
2055
 
2056
static CORE_ADDR
2057
call0_analyze_prologue (CORE_ADDR start, CORE_ADDR pc,
2058
                        int nregs, xtensa_c0reg_t rt[], int *call0)
2059
{
2060
  CORE_ADDR ia;             /* Current insn address in prologue.  */
2061
  CORE_ADDR ba = 0;          /* Current address at base of insn buffer.  */
2062
  CORE_ADDR bt;             /* Current address at top+1 of insn buffer.  */
2063
  #define BSZ 32            /* Instruction buffer size.  */
2064
  char ibuf[BSZ];           /* Instruction buffer for decoding prologue.  */
2065
  xtensa_isa isa;           /* libisa ISA handle.  */
2066
  xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot.  */
2067
  xtensa_format ifmt;       /* libisa instruction format.  */
2068
  int ilen, islots, is;     /* Instruction length, nbr slots, current slot.  */
2069
  xtensa_opcode opc;        /* Opcode in current slot.  */
2070
  xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis.  */
2071
  int nods;                 /* Opcode number of operands.  */
2072
  unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa.  */
2073
  xtensa_c0reg_t *rtmp;     /* Register tracking info snapshot.  */
2074
  int j;                    /* General loop counter.  */
2075
  int fail = 0;              /* Set non-zero and exit, if decoding fails.  */
2076
  CORE_ADDR body_pc;        /* The PC for the first non-prologue insn.  */
2077
  CORE_ADDR end_pc;         /* The PC for the lust function insn.  */
2078
 
2079
  struct symtab_and_line prologue_sal;
2080
 
2081
  DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
2082
              (int)start, (int)pc);
2083
 
2084
  /* Try to limit the scan to the end of the function if a non-zero pc
2085
     arg was not supplied to avoid probing beyond the end of valid memory.
2086
     If memory is full of garbage that classifies as c0opc_uninteresting.
2087
     If this fails (eg. if no symbols) pc ends up 0 as it was.
2088
     Intialize the Call0 frame and register tracking info.
2089
     Assume it's Call0 until an 'entry' instruction is encountered.
2090
     Assume we may be in the prologue until we hit a flow control instr.  */
2091
 
2092
  rtmp = NULL;
2093
  body_pc = INT_MAX;
2094
  end_pc = 0;
2095
 
2096
  /* Find out, if we have an information about the prologue from DWARF.  */
2097
  prologue_sal = find_pc_line (start, 0);
2098
  if (prologue_sal.line != 0) /* Found debug info.  */
2099
    body_pc = prologue_sal.end;
2100
 
2101
  /* If we are going to analyze the prologue in general without knowing about
2102
     the current PC, make the best assumtion for the end of the prologue.  */
2103
  if (pc == 0)
2104
    {
2105
      find_pc_partial_function (start, 0, NULL, &end_pc);
2106
      body_pc = min (end_pc, body_pc);
2107
    }
2108
  else
2109
    body_pc = min (pc, body_pc);
2110
 
2111
  if (call0 != NULL)
2112
      *call0 = 1;
2113
 
2114
  if (rt != NULL)
2115
    {
2116
      rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
2117
      /* rt is already initialized in xtensa_alloc_frame_cache().  */
2118
    }
2119
  else nregs = 0;
2120
 
2121
  if (!xtensa_default_isa)
2122
    xtensa_default_isa = xtensa_isa_init (0, 0);
2123
  isa = xtensa_default_isa;
2124
  gdb_assert (BSZ >= xtensa_isa_maxlength (isa));
2125
  ins = xtensa_insnbuf_alloc (isa);
2126
  slot = xtensa_insnbuf_alloc (isa);
2127
 
2128
  for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
2129
    {
2130
      /* (Re)fill instruction buffer from memory if necessary, but do not
2131
         read memory beyond PC to be sure we stay within text section
2132
         (this protection only works if a non-zero pc is supplied).  */
2133
 
2134
      if (ia + xtensa_isa_maxlength (isa) > bt)
2135
        {
2136
          ba = ia;
2137
          bt = (ba + BSZ) < body_pc ? ba + BSZ : body_pc;
2138
          read_memory (ba, ibuf, bt - ba);
2139
        }
2140
 
2141
      /* Decode format information.  */
2142
 
2143
      xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2144
      ifmt = xtensa_format_decode (isa, ins);
2145
      if (ifmt == XTENSA_UNDEFINED)
2146
        {
2147
          fail = 1;
2148
          goto done;
2149
        }
2150
      ilen = xtensa_format_length (isa, ifmt);
2151
      if (ilen == XTENSA_UNDEFINED)
2152
        {
2153
          fail = 1;
2154
          goto done;
2155
        }
2156
      islots = xtensa_format_num_slots (isa, ifmt);
2157
      if (islots == XTENSA_UNDEFINED)
2158
        {
2159
          fail = 1;
2160
          goto done;
2161
        }
2162
 
2163
      /* Analyze a bundle or a single instruction, using a snapshot of
2164
         the register tracking info as input for the entire bundle so that
2165
         register changes do not take effect within this bundle.  */
2166
 
2167
      for (j = 0; j < nregs; ++j)
2168
        rtmp[j] = rt[j];
2169
 
2170
      for (is = 0; is < islots; ++is)
2171
        {
2172
          /* Decode a slot and classify the opcode.  */
2173
 
2174
          fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
2175
          if (fail)
2176
            goto done;
2177
 
2178
          opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2179
          DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
2180
                     (unsigned)ia, opc);
2181
          if (opc == XTENSA_UNDEFINED)
2182
            opclass = c0opc_illegal;
2183
          else
2184
            opclass = call0_classify_opcode (isa, opc);
2185
 
2186
          /* Decide whether to track this opcode, ignore it, or bail out.  */
2187
 
2188
          switch (opclass)
2189
            {
2190
            case c0opc_illegal:
2191
            case c0opc_break:
2192
              fail = 1;
2193
              goto done;
2194
 
2195
            case c0opc_uninteresting:
2196
              continue;
2197
 
2198
            case c0opc_flow:
2199
              goto done;
2200
 
2201
            case c0opc_entry:
2202
              if (call0 != NULL)
2203
                *call0 = 0;
2204
              ia += ilen;               /* Skip over 'entry' insn.  */
2205
              goto done;
2206
 
2207
            default:
2208
              if (call0 != NULL)
2209
                *call0 = 1;
2210
            }
2211
 
2212
          /* Only expected opcodes should get this far.  */
2213
          if (rt == NULL)
2214
            continue;
2215
 
2216
          /* Extract and decode the operands.  */
2217
          nods = xtensa_opcode_num_operands (isa, opc);
2218
          if (nods == XTENSA_UNDEFINED)
2219
            {
2220
              fail = 1;
2221
              goto done;
2222
            }
2223
 
2224
          for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
2225
            {
2226
              fail = xtensa_operand_get_field (isa, opc, j, ifmt,
2227
                                               is, slot, &odv[j]);
2228
              if (fail)
2229
                goto done;
2230
 
2231
              fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
2232
              if (fail)
2233
                goto done;
2234
            }
2235
 
2236
          /* Check operands to verify use of 'mov' assembler macro.  */
2237
          if (opclass == c0opc_mov && nods == 3)
2238
            {
2239
              if (odv[2] == odv[1])
2240
                nods = 2;
2241
              else
2242
                {
2243
                  opclass = c0opc_uninteresting;
2244
                  continue;
2245
                }
2246
            }
2247
 
2248
          /* Track register movement and modification for this operation.  */
2249
          call0_track_op (rt, rtmp, opclass, nods, odv, ia, 1);
2250
        }
2251
    }
2252
done:
2253
  DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
2254
             (unsigned)ia, fail ? "failed" : "succeeded");
2255
  xtensa_insnbuf_free(isa, slot);
2256
  xtensa_insnbuf_free(isa, ins);
2257
  return fail ? 0 : ia;
2258
}
2259
 
2260
/* Initialize frame cache for the current frame.  The "next_frame" is the next
2261
   one relative to current frame.  "cache" is the pointer to the data structure
2262
   we have to initialize.  "pc" is curretnt PC.  */
2263
 
2264
static void
2265
call0_frame_cache (struct frame_info *next_frame,
2266
                   xtensa_frame_cache_t *cache, CORE_ADDR pc)
2267
{
2268
  struct gdbarch *gdbarch = get_frame_arch (next_frame);
2269
  CORE_ADDR start_pc;           /* The beginning of the function.  */
2270
  CORE_ADDR body_pc=UINT_MAX;   /* PC, where prologue analysis stopped.  */
2271
  CORE_ADDR sp, fp, ra;
2272
  int fp_regnum, c0_hasfp, c0_frmsz, prev_sp, to_stk;
2273
 
2274
  /* Find the beginning of the prologue of the function containing the PC
2275
     and analyze it up to the PC or the end of the prologue.  */
2276
 
2277
  if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
2278
    {
2279
      body_pc = call0_analyze_prologue (start_pc, pc, C0_NREGS,
2280
                                        &cache->c0.c0_rt[0],
2281
                                        &cache->call0);
2282
    }
2283
 
2284
  sp = frame_unwind_register_unsigned
2285
         (next_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
2286
  fp = sp; /* Assume FP == SP until proven otherwise.  */
2287
 
2288
  /* Get the frame information and FP (if used) at the current PC.
2289
     If PC is in the prologue, the prologue analysis is more reliable
2290
     than DWARF info.  We don't not know for sure if PC is in the prologue,
2291
     but we know no calls have yet taken place, so we can almost
2292
     certainly rely on the prologue analysis.  */
2293
 
2294
  if (body_pc <= pc)
2295
    {
2296
      /* Prologue analysis was successful up to the PC.
2297
         It includes the cases when PC == START_PC.  */
2298
      c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
2299
      /* c0_hasfp == true means there is a frame pointer because
2300
         we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
2301
         was derived from SP.  Otherwise, it would be C0_FP.  */
2302
      fp_regnum = c0_hasfp ? C0_FP : C0_SP;
2303
      c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
2304
      fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
2305
    }
2306
  else  /* No data from the prologue analysis.  */
2307
    {
2308
      c0_hasfp = 0;
2309
      fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
2310
      c0_frmsz = 0;
2311
      start_pc = pc;
2312
   }
2313
 
2314
  prev_sp = fp + c0_frmsz;
2315
 
2316
  /* Frame size from debug info or prologue tracking does not account for
2317
     alloca() and other dynamic allocations.  Adjust frame size by FP - SP.  */
2318
  if (c0_hasfp)
2319
    {
2320
      fp = frame_unwind_register_unsigned (next_frame, fp_regnum);
2321
 
2322
      /* Recalculate previous SP.  */
2323
      prev_sp = fp + c0_frmsz;
2324
      /* Update the stack frame size.  */
2325
      c0_frmsz += fp - sp;
2326
    }
2327
 
2328
  /* Get the return address (RA) from the stack if saved,
2329
     or try to get it from a register.  */
2330
 
2331
  to_stk = cache->c0.c0_rt[C0_RA].to_stk;
2332
  if (to_stk != C0_NOSTK)
2333
    ra = (CORE_ADDR)
2334
      read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk, 4);
2335
 
2336
  else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
2337
           && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
2338
    {
2339
      /* Special case for terminating backtrace at a function that wants to
2340
         be seen as the outermost.  Such a function will clear it's RA (A0)
2341
         register to 0 in the prologue instead of saving its original value.  */
2342
      ra = 0;
2343
    }
2344
  else
2345
    {
2346
      /* RA was copied to another register or (before any function call) may
2347
         still be in the original RA register.  This is not always reliable:
2348
         even in a leaf function, register tracking stops after prologue, and
2349
         even in prologue, non-prologue instructions (not tracked) may overwrite
2350
         RA or any register it was copied to.  If likely in prologue or before
2351
         any call, use retracking info and hope for the best (compiler should
2352
         have saved RA in stack if not in a leaf function).  If not in prologue,
2353
         too bad.  */
2354
 
2355
      int i;
2356
      for (i = 0;
2357
           (i < C0_NREGS) &&
2358
             (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
2359
           ++i);
2360
      if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
2361
        i = C0_RA;
2362
      if (i < C0_NREGS) /* Read from the next_frame.  */
2363
        {
2364
          ra = frame_unwind_register_unsigned
2365
                 (next_frame,
2366
                  gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
2367
        }
2368
      else ra = 0;
2369
    }
2370
 
2371
  cache->pc = start_pc;
2372
  cache->ra = ra;
2373
  /* RA == 0 marks the outermost frame.  Do not go past it.  */
2374
  cache->prev_sp = (ra != 0) ?  prev_sp : 0;
2375
  cache->c0.fp_regnum = fp_regnum;
2376
  cache->c0.c0_frmsz = c0_frmsz;
2377
  cache->c0.c0_hasfp = c0_hasfp;
2378
  cache->c0.c0_fp = fp;
2379
}
2380
 
2381
 
2382
/* Skip function prologue.
2383
 
2384
   Return the pc of the first instruction after prologue.  GDB calls this to
2385
   find the address of the first line of the function or (if there is no line
2386
   number information) to skip the prologue for planting breakpoints on
2387
   function entries.  Use debug info (if present) or prologue analysis to skip
2388
   the prologue to achieve reliable debugging behavior.  For windowed ABI,
2389
   only the 'entry' instruction is skipped.  It is not strictly necessary to
2390
   skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
2391
   backtrace at any point in the prologue, however certain potential hazards
2392
   are avoided and a more "normal" debugging experience is ensured by
2393
   skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
2394
   For example, if we don't skip the prologue:
2395
   - Some args may not yet have been saved to the stack where the debug
2396
     info expects to find them (true anyway when only 'entry' is skipped);
2397
   - Software breakpoints ('break' instrs) may not have been unplanted
2398
     when the prologue analysis is done on initializing the frame cache,
2399
     and breaks in the prologue will throw off the analysis.
2400
 
2401
   If we have debug info ( line-number info, in particular ) we simply skip
2402
   the code associated with the first function line effectively skipping
2403
   the prologue code.  It works even in cases like
2404
 
2405
   int main()
2406
   {    int local_var = 1;
2407
        ....
2408
   }
2409
 
2410
   because, for this source code, both Xtensa compilers will generate two
2411
   separate entries ( with the same line number ) in dwarf line-number
2412
   section to make sure there is a boundary between the prologue code and
2413
   the rest of the function.
2414
 
2415
   If there is no debug info, we need to analyze the code.  */
2416
 
2417
/* #define DONT_SKIP_PROLOGUE  */
2418
 
2419
CORE_ADDR
2420
xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
2421
{
2422
  struct symtab_and_line prologue_sal;
2423
  CORE_ADDR body_pc;
2424
 
2425
  DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
2426
 
2427
#if DONT_SKIP_PROLOGUE
2428
  return start_pc;
2429
#endif
2430
 
2431
 /* Try to find first body line from debug info.  */
2432
 
2433
  prologue_sal = find_pc_line (start_pc, 0);
2434
  if (prologue_sal.line != 0) /* Found debug info.  */
2435
    {
2436
      /* In Call0, it is possible to have a function with only one instruction
2437
         ('ret') resulting from a 1-line optimized function that does nothing.
2438
         In that case, prologue_sal.end may actually point to the start of the
2439
         next function in the text section, causing a breakpoint to be set at
2440
         the wrong place.  Check if the end address is in a different function,
2441
         and if so return the start PC.  We know we have symbol info.  */
2442
 
2443
      CORE_ADDR end_func;
2444
 
2445
      find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
2446
      if (end_func != start_pc)
2447
        return start_pc;
2448
 
2449
      return prologue_sal.end;
2450
    }
2451
 
2452
  /* No debug line info.  Analyze prologue for Call0 or simply skip ENTRY.  */
2453
  body_pc = call0_analyze_prologue(start_pc, 0, 0, NULL, NULL);
2454
  return body_pc != 0 ? body_pc : start_pc;
2455
}
2456
 
2457
/* Verify the current configuration.  */
2458
static void
2459
xtensa_verify_config (struct gdbarch *gdbarch)
2460
{
2461
  struct ui_file *log;
2462
  struct cleanup *cleanups;
2463
  struct gdbarch_tdep *tdep;
2464
  long dummy;
2465
  char *buf;
2466
 
2467
  tdep = gdbarch_tdep (gdbarch);
2468
  log = mem_fileopen ();
2469
  cleanups = make_cleanup_ui_file_delete (log);
2470
 
2471
  /* Verify that we got a reasonable number of AREGS.  */
2472
  if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
2473
    fprintf_unfiltered (log, _("\
2474
\n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
2475
                        tdep->num_aregs);
2476
 
2477
  /* Verify that certain registers exist.  */
2478
 
2479
  if (tdep->pc_regnum == -1)
2480
    fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
2481
  if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
2482
    fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));
2483
 
2484
  if (tdep->isa_use_windowed_registers)
2485
    {
2486
      if (tdep->wb_regnum == -1)
2487
        fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
2488
      if (tdep->ws_regnum == -1)
2489
        fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
2490
      if (tdep->ar_base == -1)
2491
        fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
2492
    }
2493
 
2494
  if (tdep->a0_base == -1)
2495
    fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
2496
 
2497
  buf = ui_file_xstrdup (log, &dummy);
2498
  make_cleanup (xfree, buf);
2499
  if (strlen (buf) > 0)
2500
    internal_error (__FILE__, __LINE__,
2501
                    _("the following are invalid: %s"), buf);
2502
  do_cleanups (cleanups);
2503
}
2504
 
2505
 
2506
/* Derive specific register numbers from the array of registers.  */
2507
 
2508
void
2509
xtensa_derive_tdep (struct gdbarch_tdep *tdep)
2510
{
2511
  xtensa_register_t* rmap;
2512
  int n, max_size = 4;
2513
 
2514
  tdep->num_regs = 0;
2515
  tdep->num_nopriv_regs = 0;
2516
 
2517
/* Special registers 0..255 (core).  */
2518
#define XTENSA_DBREGN_SREG(n)  (0x0200+(n))
2519
 
2520
  for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
2521
    {
2522
      if (rmap->target_number == 0x0020)
2523
        tdep->pc_regnum = n;
2524
      else if (rmap->target_number == 0x0100)
2525
        tdep->ar_base = n;
2526
      else if (rmap->target_number == 0x0000)
2527
        tdep->a0_base = n;
2528
      else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
2529
        tdep->wb_regnum = n;
2530
      else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
2531
        tdep->ws_regnum = n;
2532
      else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
2533
        tdep->debugcause_regnum = n;
2534
      else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
2535
        tdep->exccause_regnum = n;
2536
      else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
2537
        tdep->excvaddr_regnum = n;
2538
      else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
2539
        tdep->lbeg_regnum = n;
2540
      else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
2541
        tdep->lend_regnum = n;
2542
      else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
2543
        tdep->lcount_regnum = n;
2544
      else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
2545
        tdep->sar_regnum = n;
2546
      else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
2547
        tdep->litbase_regnum = n;
2548
      else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
2549
        tdep->ps_regnum = n;
2550
#if 0
2551
      else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
2552
        tdep->interrupt_regnum = n;
2553
      else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
2554
        tdep->interrupt2_regnum = n;
2555
      else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
2556
        tdep->cpenable_regnum = n;
2557
#endif
2558
 
2559
      if (rmap->byte_size > max_size)
2560
        max_size = rmap->byte_size;
2561
      if (rmap->mask != 0 && tdep->num_regs == 0)
2562
        tdep->num_regs = n;
2563
      /* Find out out how to deal with priveleged registers.
2564
 
2565
         if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
2566
              && tdep->num_nopriv_regs == 0)
2567
           tdep->num_nopriv_regs = n;
2568
      */
2569
      if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
2570
          && tdep->num_regs == 0)
2571
        tdep->num_regs = n;
2572
    }
2573
 
2574
  /* Number of pseudo registers.  */
2575
  tdep->num_pseudo_regs = n - tdep->num_regs;
2576
 
2577
  /* Empirically determined maximum sizes.  */
2578
  tdep->max_register_raw_size = max_size;
2579
  tdep->max_register_virtual_size = max_size;
2580
}
2581
 
2582
/* Module "constructor" function.  */
2583
 
2584
extern struct gdbarch_tdep xtensa_tdep;
2585
 
2586
static struct gdbarch *
2587
xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2588
{
2589
  struct gdbarch_tdep *tdep;
2590
  struct gdbarch *gdbarch;
2591
  struct xtensa_abi_handler *abi_handler;
2592
 
2593
  DEBUGTRACE ("gdbarch_init()\n");
2594
 
2595
  /* We have to set the byte order before we call gdbarch_alloc.  */
2596
  info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
2597
 
2598
  tdep = &xtensa_tdep;
2599
  gdbarch = gdbarch_alloc (&info, tdep);
2600
  xtensa_derive_tdep (tdep);
2601
 
2602
  /* Verify our configuration.  */
2603
  xtensa_verify_config (gdbarch);
2604
 
2605
  /* Pseudo-Register read/write.  */
2606
  set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
2607
  set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
2608
 
2609
  /* Set target information.  */
2610
  set_gdbarch_num_regs (gdbarch, tdep->num_regs);
2611
  set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
2612
  set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
2613
  set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
2614
  set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
2615
 
2616
  /* Renumber registers for known formats (stab, dwarf, and dwarf2).  */
2617
  set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2618
  set_gdbarch_dwarf_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2619
  set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2620
 
2621
  /* We provide our own function to get register information.  */
2622
  set_gdbarch_register_name (gdbarch, xtensa_register_name);
2623
  set_gdbarch_register_type (gdbarch, xtensa_register_type);
2624
 
2625
  /* To call functions from GDB using dummy frame */
2626
  set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
2627
 
2628
  set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2629
 
2630
  set_gdbarch_return_value (gdbarch, xtensa_return_value);
2631
 
2632
  /* Advance PC across any prologue instructions to reach "real" code.  */
2633
  set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
2634
 
2635
  /* Stack grows downward.  */
2636
  set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2637
 
2638
  /* Set breakpoints.  */
2639
  set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
2640
 
2641
  /* After breakpoint instruction or illegal instruction, pc still
2642
     points at break instruction, so don't decrement.  */
2643
  set_gdbarch_decr_pc_after_break (gdbarch, 0);
2644
 
2645
  /* We don't skip args.  */
2646
  set_gdbarch_frame_args_skip (gdbarch, 0);
2647
 
2648
  set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
2649
 
2650
  set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
2651
 
2652
  set_gdbarch_unwind_dummy_id (gdbarch, xtensa_unwind_dummy_id);
2653
 
2654
  /* Frame handling.  */
2655
  frame_base_set_default (gdbarch, &xtensa_frame_base);
2656
  frame_unwind_append_sniffer (gdbarch, xtensa_frame_sniffer);
2657
 
2658
  set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
2659
 
2660
  set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
2661
 
2662
  xtensa_add_reggroups (gdbarch);
2663
  set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
2664
 
2665
  set_gdbarch_regset_from_core_section (gdbarch,
2666
                                        xtensa_regset_from_core_section);
2667
 
2668
  return gdbarch;
2669
}
2670
 
2671
static void
2672
xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
2673
{
2674
  error (_("xtensa_dump_tdep(): not implemented"));
2675
}
2676
 
2677
void
2678
_initialize_xtensa_tdep (void)
2679
{
2680
  struct cmd_list_element *c;
2681
 
2682
  gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
2683
  xtensa_init_reggroups ();
2684
 
2685
  add_setshow_zinteger_cmd ("xtensa",
2686
                            class_maintenance,
2687
                            &xtensa_debug_level, _("\
2688
Set Xtensa debugging."), _("\
2689
Show Xtensa debugging."), _("\
2690
When non-zero, Xtensa-specific debugging is enabled. \
2691
Can be 1, 2, 3, or 4 indicating the level of debugging."),
2692
                            NULL,
2693
                            NULL,
2694
                            &setdebuglist, &showdebuglist);
2695
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.