xtensa-tdep.c revision 1.6 1 1.1 christos /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
2 1.1 christos
3 1.6 christos Copyright (C) 2003-2016 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos #include "frame.h"
22 1.1 christos #include "solib-svr4.h"
23 1.1 christos #include "symtab.h"
24 1.1 christos #include "symfile.h"
25 1.1 christos #include "objfiles.h"
26 1.1 christos #include "gdbtypes.h"
27 1.1 christos #include "gdbcore.h"
28 1.1 christos #include "value.h"
29 1.1 christos #include "dis-asm.h"
30 1.1 christos #include "inferior.h"
31 1.6 christos #include "osabi.h"
32 1.1 christos #include "floatformat.h"
33 1.1 christos #include "regcache.h"
34 1.1 christos #include "reggroups.h"
35 1.1 christos #include "regset.h"
36 1.1 christos
37 1.1 christos #include "dummy-frame.h"
38 1.1 christos #include "dwarf2.h"
39 1.1 christos #include "dwarf2-frame.h"
40 1.1 christos #include "dwarf2loc.h"
41 1.1 christos #include "frame-base.h"
42 1.1 christos #include "frame-unwind.h"
43 1.1 christos
44 1.1 christos #include "arch-utils.h"
45 1.1 christos #include "gdbarch.h"
46 1.1 christos #include "remote.h"
47 1.1 christos #include "serial.h"
48 1.1 christos
49 1.1 christos #include "command.h"
50 1.1 christos #include "gdbcmd.h"
51 1.1 christos
52 1.1 christos #include "xtensa-isa.h"
53 1.1 christos #include "xtensa-tdep.h"
54 1.1 christos #include "xtensa-config.h"
55 1.1 christos
56 1.1 christos
57 1.1 christos static unsigned int xtensa_debug_level = 0;
58 1.1 christos
59 1.1 christos #define DEBUGWARN(args...) \
60 1.1 christos if (xtensa_debug_level > 0) \
61 1.1 christos fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
62 1.1 christos
63 1.1 christos #define DEBUGINFO(args...) \
64 1.1 christos if (xtensa_debug_level > 1) \
65 1.1 christos fprintf_unfiltered (gdb_stdlog, "(info ) " args)
66 1.1 christos
67 1.1 christos #define DEBUGTRACE(args...) \
68 1.1 christos if (xtensa_debug_level > 2) \
69 1.1 christos fprintf_unfiltered (gdb_stdlog, "(trace) " args)
70 1.1 christos
71 1.1 christos #define DEBUGVERB(args...) \
72 1.1 christos if (xtensa_debug_level > 3) \
73 1.1 christos fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
74 1.1 christos
75 1.1 christos
76 1.1 christos /* According to the ABI, the SP must be aligned to 16-byte boundaries. */
77 1.1 christos #define SP_ALIGNMENT 16
78 1.1 christos
79 1.1 christos
80 1.1 christos /* On Windowed ABI, we use a6 through a11 for passing arguments
81 1.1 christos to a function called by GDB because CALL4 is used. */
82 1.1 christos #define ARGS_NUM_REGS 6
83 1.1 christos #define REGISTER_SIZE 4
84 1.1 christos
85 1.1 christos
86 1.1 christos /* Extract the call size from the return address or PS register. */
87 1.1 christos #define PS_CALLINC_SHIFT 16
88 1.1 christos #define PS_CALLINC_MASK 0x00030000
89 1.1 christos #define CALLINC(ps) (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
90 1.1 christos #define WINSIZE(ra) (4 * (( (ra) >> 30) & 0x3))
91 1.1 christos
92 1.1 christos /* On TX, hardware can be configured without Exception Option.
93 1.1 christos There is no PS register in this case. Inside XT-GDB, let us treat
94 1.1 christos it as a virtual read-only register always holding the same value. */
95 1.1 christos #define TX_PS 0x20
96 1.1 christos
97 1.1 christos /* ABI-independent macros. */
98 1.1 christos #define ARG_NOF(gdbarch) \
99 1.1 christos (gdbarch_tdep (gdbarch)->call_abi \
100 1.1 christos == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
101 1.1 christos #define ARG_1ST(gdbarch) \
102 1.1 christos (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only \
103 1.1 christos ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
104 1.1 christos : (gdbarch_tdep (gdbarch)->a0_base + 6))
105 1.1 christos
106 1.1 christos /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
107 1.1 christos indicates that the instruction is an ENTRY instruction. */
108 1.1 christos
109 1.1 christos #define XTENSA_IS_ENTRY(gdbarch, op1) \
110 1.1 christos ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
111 1.1 christos ? ((op1) == 0x6c) : ((op1) == 0x36))
112 1.1 christos
113 1.1 christos #define XTENSA_ENTRY_LENGTH 3
114 1.1 christos
115 1.1 christos /* windowing_enabled() returns true, if windowing is enabled.
116 1.1 christos WOE must be set to 1; EXCM to 0.
117 1.1 christos Note: We assume that EXCM is always 0 for XEA1. */
118 1.1 christos
119 1.1 christos #define PS_WOE (1<<18)
120 1.1 christos #define PS_EXC (1<<4)
121 1.1 christos
122 1.1 christos static int
123 1.1 christos windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
124 1.1 christos {
125 1.1 christos /* If we know CALL0 ABI is set explicitly, say it is Call0. */
126 1.1 christos if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
127 1.1 christos return 0;
128 1.1 christos
129 1.1 christos return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
130 1.1 christos }
131 1.1 christos
132 1.1 christos /* Convert a live A-register number to the corresponding AR-register
133 1.1 christos number. */
134 1.1 christos static int
135 1.1 christos arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
136 1.1 christos {
137 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
138 1.1 christos int arreg;
139 1.1 christos
140 1.1 christos arreg = a_regnum - tdep->a0_base;
141 1.1 christos arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
142 1.1 christos arreg &= tdep->num_aregs - 1;
143 1.1 christos
144 1.1 christos return arreg + tdep->ar_base;
145 1.1 christos }
146 1.1 christos
147 1.1 christos /* Convert a live AR-register number to the corresponding A-register order
148 1.1 christos number in a range [0..15]. Return -1, if AR_REGNUM is out of WB window. */
149 1.1 christos static int
150 1.1 christos areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
151 1.1 christos {
152 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
153 1.1 christos int areg;
154 1.1 christos
155 1.1 christos areg = ar_regnum - tdep->ar_base;
156 1.1 christos if (areg < 0 || areg >= tdep->num_aregs)
157 1.1 christos return -1;
158 1.1 christos areg = (areg - wb * 4) & (tdep->num_aregs - 1);
159 1.1 christos return (areg > 15) ? -1 : areg;
160 1.1 christos }
161 1.1 christos
162 1.1 christos /* Read Xtensa register directly from the hardware. */
163 1.1 christos static unsigned long
164 1.1 christos xtensa_read_register (int regnum)
165 1.1 christos {
166 1.1 christos ULONGEST value;
167 1.1 christos
168 1.1 christos regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
169 1.1 christos return (unsigned long) value;
170 1.1 christos }
171 1.1 christos
172 1.1 christos /* Write Xtensa register directly to the hardware. */
173 1.1 christos static void
174 1.1 christos xtensa_write_register (int regnum, ULONGEST value)
175 1.1 christos {
176 1.1 christos regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
177 1.1 christos }
178 1.1 christos
179 1.1 christos /* Return the window size of the previous call to the function from which we
180 1.1 christos have just returned.
181 1.1 christos
182 1.1 christos This function is used to extract the return value after a called function
183 1.1 christos has returned to the caller. On Xtensa, the register that holds the return
184 1.1 christos value (from the perspective of the caller) depends on what call
185 1.1 christos instruction was used. For now, we are assuming that the call instruction
186 1.1 christos precedes the current address, so we simply analyze the call instruction.
187 1.1 christos If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
188 1.1 christos method to call the inferior function. */
189 1.1 christos
190 1.1 christos static int
191 1.1 christos extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
192 1.1 christos {
193 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
194 1.1 christos int winsize = 4;
195 1.1 christos int insn;
196 1.1 christos gdb_byte buf[4];
197 1.1 christos
198 1.1 christos DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
199 1.1 christos
200 1.1 christos /* Read the previous instruction (should be a call[x]{4|8|12}. */
201 1.1 christos read_memory (pc-3, buf, 3);
202 1.1 christos insn = extract_unsigned_integer (buf, 3, byte_order);
203 1.1 christos
204 1.1 christos /* Decode call instruction:
205 1.1 christos Little Endian
206 1.1 christos call{0,4,8,12} OFFSET || {00,01,10,11} || 0101
207 1.1 christos callx{0,4,8,12} OFFSET || 11 || {00,01,10,11} || 0000
208 1.1 christos Big Endian
209 1.1 christos call{0,4,8,12} 0101 || {00,01,10,11} || OFFSET
210 1.1 christos callx{0,4,8,12} 0000 || {00,01,10,11} || 11 || OFFSET. */
211 1.1 christos
212 1.1 christos if (byte_order == BFD_ENDIAN_LITTLE)
213 1.1 christos {
214 1.1 christos if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
215 1.1 christos winsize = (insn & 0x30) >> 2; /* 0, 4, 8, 12. */
216 1.1 christos }
217 1.1 christos else
218 1.1 christos {
219 1.1 christos if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
220 1.1 christos winsize = (insn >> 16) & 0xc; /* 0, 4, 8, 12. */
221 1.1 christos }
222 1.1 christos return winsize;
223 1.1 christos }
224 1.1 christos
225 1.1 christos
226 1.1 christos /* REGISTER INFORMATION */
227 1.1 christos
228 1.1 christos /* Find register by name. */
229 1.1 christos static int
230 1.1 christos xtensa_find_register_by_name (struct gdbarch *gdbarch, char *name)
231 1.1 christos {
232 1.1 christos int i;
233 1.1 christos
234 1.1 christos for (i = 0; i < gdbarch_num_regs (gdbarch)
235 1.1 christos + gdbarch_num_pseudo_regs (gdbarch);
236 1.1 christos i++)
237 1.1 christos
238 1.1 christos if (strcasecmp (gdbarch_tdep (gdbarch)->regmap[i].name, name) == 0)
239 1.1 christos return i;
240 1.1 christos
241 1.1 christos return -1;
242 1.1 christos }
243 1.1 christos
244 1.1 christos /* Returns the name of a register. */
245 1.1 christos static const char *
246 1.1 christos xtensa_register_name (struct gdbarch *gdbarch, int regnum)
247 1.1 christos {
248 1.1 christos /* Return the name stored in the register map. */
249 1.1 christos if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
250 1.1 christos + gdbarch_num_pseudo_regs (gdbarch))
251 1.1 christos return gdbarch_tdep (gdbarch)->regmap[regnum].name;
252 1.1 christos
253 1.1 christos internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
254 1.1 christos return 0;
255 1.1 christos }
256 1.1 christos
257 1.1 christos /* Return the type of a register. Create a new type, if necessary. */
258 1.1 christos
259 1.1 christos static struct type *
260 1.1 christos xtensa_register_type (struct gdbarch *gdbarch, int regnum)
261 1.1 christos {
262 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
263 1.1 christos
264 1.1 christos /* Return signed integer for ARx and Ax registers. */
265 1.1 christos if ((regnum >= tdep->ar_base
266 1.1 christos && regnum < tdep->ar_base + tdep->num_aregs)
267 1.1 christos || (regnum >= tdep->a0_base
268 1.1 christos && regnum < tdep->a0_base + 16))
269 1.1 christos return builtin_type (gdbarch)->builtin_int;
270 1.1 christos
271 1.1 christos if (regnum == gdbarch_pc_regnum (gdbarch)
272 1.1 christos || regnum == tdep->a0_base + 1)
273 1.1 christos return builtin_type (gdbarch)->builtin_data_ptr;
274 1.1 christos
275 1.1 christos /* Return the stored type for all other registers. */
276 1.1 christos else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
277 1.1 christos + gdbarch_num_pseudo_regs (gdbarch))
278 1.1 christos {
279 1.1 christos xtensa_register_t* reg = &tdep->regmap[regnum];
280 1.1 christos
281 1.1 christos /* Set ctype for this register (only the first time). */
282 1.1 christos
283 1.1 christos if (reg->ctype == 0)
284 1.1 christos {
285 1.1 christos struct ctype_cache *tp;
286 1.1 christos int size = reg->byte_size;
287 1.1 christos
288 1.1 christos /* We always use the memory representation,
289 1.1 christos even if the register width is smaller. */
290 1.1 christos switch (size)
291 1.1 christos {
292 1.1 christos case 1:
293 1.1 christos reg->ctype = builtin_type (gdbarch)->builtin_uint8;
294 1.1 christos break;
295 1.1 christos
296 1.1 christos case 2:
297 1.1 christos reg->ctype = builtin_type (gdbarch)->builtin_uint16;
298 1.1 christos break;
299 1.1 christos
300 1.1 christos case 4:
301 1.1 christos reg->ctype = builtin_type (gdbarch)->builtin_uint32;
302 1.1 christos break;
303 1.1 christos
304 1.1 christos case 8:
305 1.1 christos reg->ctype = builtin_type (gdbarch)->builtin_uint64;
306 1.1 christos break;
307 1.1 christos
308 1.1 christos case 16:
309 1.1 christos reg->ctype = builtin_type (gdbarch)->builtin_uint128;
310 1.1 christos break;
311 1.1 christos
312 1.1 christos default:
313 1.1 christos for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
314 1.1 christos if (tp->size == size)
315 1.1 christos break;
316 1.1 christos
317 1.1 christos if (tp == NULL)
318 1.1 christos {
319 1.1 christos char *name = xstrprintf ("int%d", size * 8);
320 1.6 christos
321 1.6 christos tp = XNEW (struct ctype_cache);
322 1.1 christos tp->next = tdep->type_entries;
323 1.1 christos tdep->type_entries = tp;
324 1.1 christos tp->size = size;
325 1.1 christos tp->virtual_type
326 1.1 christos = arch_integer_type (gdbarch, size * 8, 1, name);
327 1.1 christos xfree (name);
328 1.1 christos }
329 1.1 christos
330 1.1 christos reg->ctype = tp->virtual_type;
331 1.1 christos }
332 1.1 christos }
333 1.1 christos return reg->ctype;
334 1.1 christos }
335 1.1 christos
336 1.1 christos internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
337 1.1 christos return 0;
338 1.1 christos }
339 1.1 christos
340 1.1 christos
341 1.1 christos /* Return the 'local' register number for stubs, dwarf2, etc.
342 1.1 christos The debugging information enumerates registers starting from 0 for A0
343 1.1 christos to n for An. So, we only have to add the base number for A0. */
344 1.1 christos
345 1.1 christos static int
346 1.1 christos xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
347 1.1 christos {
348 1.1 christos int i;
349 1.1 christos
350 1.1 christos if (regnum >= 0 && regnum < 16)
351 1.1 christos return gdbarch_tdep (gdbarch)->a0_base + regnum;
352 1.1 christos
353 1.1 christos for (i = 0;
354 1.1 christos i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
355 1.1 christos i++)
356 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
357 1.1 christos return i;
358 1.1 christos
359 1.6 christos return -1;
360 1.1 christos }
361 1.1 christos
362 1.1 christos
363 1.1 christos /* Write the bits of a masked register to the various registers.
364 1.1 christos Only the masked areas of these registers are modified; the other
365 1.1 christos fields are untouched. The size of masked registers is always less
366 1.1 christos than or equal to 32 bits. */
367 1.1 christos
368 1.1 christos static void
369 1.1 christos xtensa_register_write_masked (struct regcache *regcache,
370 1.1 christos xtensa_register_t *reg, const gdb_byte *buffer)
371 1.1 christos {
372 1.1 christos unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
373 1.1 christos const xtensa_mask_t *mask = reg->mask;
374 1.1 christos
375 1.1 christos int shift = 0; /* Shift for next mask (mod 32). */
376 1.1 christos int start, size; /* Start bit and size of current mask. */
377 1.1 christos
378 1.1 christos unsigned int *ptr = value;
379 1.1 christos unsigned int regval, m, mem = 0;
380 1.1 christos
381 1.1 christos int bytesize = reg->byte_size;
382 1.1 christos int bitsize = bytesize * 8;
383 1.1 christos int i, r;
384 1.1 christos
385 1.1 christos DEBUGTRACE ("xtensa_register_write_masked ()\n");
386 1.1 christos
387 1.1 christos /* Copy the masked register to host byte-order. */
388 1.1 christos if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
389 1.1 christos for (i = 0; i < bytesize; i++)
390 1.1 christos {
391 1.1 christos mem >>= 8;
392 1.1 christos mem |= (buffer[bytesize - i - 1] << 24);
393 1.1 christos if ((i & 3) == 3)
394 1.1 christos *ptr++ = mem;
395 1.1 christos }
396 1.1 christos else
397 1.1 christos for (i = 0; i < bytesize; i++)
398 1.1 christos {
399 1.1 christos mem >>= 8;
400 1.1 christos mem |= (buffer[i] << 24);
401 1.1 christos if ((i & 3) == 3)
402 1.1 christos *ptr++ = mem;
403 1.1 christos }
404 1.1 christos
405 1.1 christos /* We might have to shift the final value:
406 1.1 christos bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
407 1.1 christos bytesize & 3 == x -> shift (4-x) * 8. */
408 1.1 christos
409 1.1 christos *ptr = mem >> (((0 - bytesize) & 3) * 8);
410 1.1 christos ptr = value;
411 1.1 christos mem = *ptr;
412 1.1 christos
413 1.1 christos /* Write the bits to the masked areas of the other registers. */
414 1.1 christos for (i = 0; i < mask->count; i++)
415 1.1 christos {
416 1.1 christos start = mask->mask[i].bit_start;
417 1.1 christos size = mask->mask[i].bit_size;
418 1.1 christos regval = mem >> shift;
419 1.1 christos
420 1.1 christos if ((shift += size) > bitsize)
421 1.1 christos error (_("size of all masks is larger than the register"));
422 1.1 christos
423 1.1 christos if (shift >= 32)
424 1.1 christos {
425 1.1 christos mem = *(++ptr);
426 1.1 christos shift -= 32;
427 1.1 christos bitsize -= 32;
428 1.1 christos
429 1.1 christos if (shift > 0)
430 1.1 christos regval |= mem << (size - shift);
431 1.1 christos }
432 1.1 christos
433 1.1 christos /* Make sure we have a valid register. */
434 1.1 christos r = mask->mask[i].reg_num;
435 1.1 christos if (r >= 0 && size > 0)
436 1.1 christos {
437 1.1 christos /* Don't overwrite the unmasked areas. */
438 1.1 christos ULONGEST old_val;
439 1.1 christos regcache_cooked_read_unsigned (regcache, r, &old_val);
440 1.1 christos m = 0xffffffff >> (32 - size) << start;
441 1.1 christos regval <<= start;
442 1.1 christos regval = (regval & m) | (old_val & ~m);
443 1.1 christos regcache_cooked_write_unsigned (regcache, r, regval);
444 1.1 christos }
445 1.1 christos }
446 1.1 christos }
447 1.1 christos
448 1.1 christos
449 1.1 christos /* Read a tie state or mapped registers. Read the masked areas
450 1.1 christos of the registers and assemble them into a single value. */
451 1.1 christos
452 1.1 christos static enum register_status
453 1.1 christos xtensa_register_read_masked (struct regcache *regcache,
454 1.1 christos xtensa_register_t *reg, gdb_byte *buffer)
455 1.1 christos {
456 1.1 christos unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
457 1.1 christos const xtensa_mask_t *mask = reg->mask;
458 1.1 christos
459 1.1 christos int shift = 0;
460 1.1 christos int start, size;
461 1.1 christos
462 1.1 christos unsigned int *ptr = value;
463 1.1 christos unsigned int regval, mem = 0;
464 1.1 christos
465 1.1 christos int bytesize = reg->byte_size;
466 1.1 christos int bitsize = bytesize * 8;
467 1.1 christos int i;
468 1.1 christos
469 1.1 christos DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
470 1.1 christos reg->name == 0 ? "" : reg->name);
471 1.1 christos
472 1.1 christos /* Assemble the register from the masked areas of other registers. */
473 1.1 christos for (i = 0; i < mask->count; i++)
474 1.1 christos {
475 1.1 christos int r = mask->mask[i].reg_num;
476 1.1 christos if (r >= 0)
477 1.1 christos {
478 1.1 christos enum register_status status;
479 1.1 christos ULONGEST val;
480 1.1 christos
481 1.1 christos status = regcache_cooked_read_unsigned (regcache, r, &val);
482 1.1 christos if (status != REG_VALID)
483 1.1 christos return status;
484 1.1 christos regval = (unsigned int) val;
485 1.1 christos }
486 1.1 christos else
487 1.1 christos regval = 0;
488 1.1 christos
489 1.1 christos start = mask->mask[i].bit_start;
490 1.1 christos size = mask->mask[i].bit_size;
491 1.1 christos
492 1.1 christos regval >>= start;
493 1.1 christos
494 1.1 christos if (size < 32)
495 1.1 christos regval &= (0xffffffff >> (32 - size));
496 1.1 christos
497 1.1 christos mem |= regval << shift;
498 1.1 christos
499 1.1 christos if ((shift += size) > bitsize)
500 1.1 christos error (_("size of all masks is larger than the register"));
501 1.1 christos
502 1.1 christos if (shift >= 32)
503 1.1 christos {
504 1.1 christos *ptr++ = mem;
505 1.1 christos bitsize -= 32;
506 1.1 christos shift -= 32;
507 1.1 christos
508 1.1 christos if (shift == 0)
509 1.1 christos mem = 0;
510 1.1 christos else
511 1.1 christos mem = regval >> (size - shift);
512 1.1 christos }
513 1.1 christos }
514 1.1 christos
515 1.1 christos if (shift > 0)
516 1.1 christos *ptr = mem;
517 1.1 christos
518 1.1 christos /* Copy value to target byte order. */
519 1.1 christos ptr = value;
520 1.1 christos mem = *ptr;
521 1.1 christos
522 1.1 christos if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
523 1.1 christos for (i = 0; i < bytesize; i++)
524 1.1 christos {
525 1.1 christos if ((i & 3) == 0)
526 1.1 christos mem = *ptr++;
527 1.1 christos buffer[bytesize - i - 1] = mem & 0xff;
528 1.1 christos mem >>= 8;
529 1.1 christos }
530 1.1 christos else
531 1.1 christos for (i = 0; i < bytesize; i++)
532 1.1 christos {
533 1.1 christos if ((i & 3) == 0)
534 1.1 christos mem = *ptr++;
535 1.1 christos buffer[i] = mem & 0xff;
536 1.1 christos mem >>= 8;
537 1.1 christos }
538 1.1 christos
539 1.1 christos return REG_VALID;
540 1.1 christos }
541 1.1 christos
542 1.1 christos
543 1.1 christos /* Read pseudo registers. */
544 1.1 christos
545 1.1 christos static enum register_status
546 1.1 christos xtensa_pseudo_register_read (struct gdbarch *gdbarch,
547 1.1 christos struct regcache *regcache,
548 1.1 christos int regnum,
549 1.1 christos gdb_byte *buffer)
550 1.1 christos {
551 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
552 1.1 christos
553 1.1 christos DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
554 1.1 christos regnum, xtensa_register_name (gdbarch, regnum));
555 1.1 christos
556 1.1 christos /* Read aliases a0..a15, if this is a Windowed ABI. */
557 1.1 christos if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
558 1.1 christos && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
559 1.1 christos && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
560 1.1 christos {
561 1.1 christos gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
562 1.1 christos enum register_status status;
563 1.1 christos
564 1.1 christos status = regcache_raw_read (regcache,
565 1.1 christos gdbarch_tdep (gdbarch)->wb_regnum,
566 1.1 christos buf);
567 1.1 christos if (status != REG_VALID)
568 1.1 christos return status;
569 1.1 christos regnum = arreg_number (gdbarch, regnum,
570 1.1 christos extract_unsigned_integer (buf, 4, byte_order));
571 1.1 christos }
572 1.1 christos
573 1.1 christos /* We can always read non-pseudo registers. */
574 1.1 christos if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
575 1.1 christos return regcache_raw_read (regcache, regnum, buffer);
576 1.1 christos
577 1.1 christos /* We have to find out how to deal with priveleged registers.
578 1.1 christos Let's treat them as pseudo-registers, but we cannot read/write them. */
579 1.1 christos
580 1.1 christos else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
581 1.1 christos {
582 1.1 christos buffer[0] = (gdb_byte)0;
583 1.1 christos buffer[1] = (gdb_byte)0;
584 1.1 christos buffer[2] = (gdb_byte)0;
585 1.1 christos buffer[3] = (gdb_byte)0;
586 1.1 christos return REG_VALID;
587 1.1 christos }
588 1.1 christos /* Pseudo registers. */
589 1.1 christos else if (regnum >= 0
590 1.1 christos && regnum < gdbarch_num_regs (gdbarch)
591 1.1 christos + gdbarch_num_pseudo_regs (gdbarch))
592 1.1 christos {
593 1.1 christos xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
594 1.1 christos xtensa_register_type_t type = reg->type;
595 1.1 christos int flags = gdbarch_tdep (gdbarch)->target_flags;
596 1.1 christos
597 1.1 christos /* We cannot read Unknown or Unmapped registers. */
598 1.1 christos if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
599 1.1 christos {
600 1.1 christos if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
601 1.1 christos {
602 1.1 christos warning (_("cannot read register %s"),
603 1.1 christos xtensa_register_name (gdbarch, regnum));
604 1.1 christos return REG_VALID;
605 1.1 christos }
606 1.1 christos }
607 1.1 christos
608 1.1 christos /* Some targets cannot read TIE register files. */
609 1.1 christos else if (type == xtRegisterTypeTieRegfile)
610 1.1 christos {
611 1.1 christos /* Use 'fetch' to get register? */
612 1.1 christos if (flags & xtTargetFlagsUseFetchStore)
613 1.1 christos {
614 1.1 christos warning (_("cannot read register"));
615 1.1 christos return REG_VALID;
616 1.1 christos }
617 1.1 christos
618 1.1 christos /* On some targets (esp. simulators), we can always read the reg. */
619 1.1 christos else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
620 1.1 christos {
621 1.1 christos warning (_("cannot read register"));
622 1.1 christos return REG_VALID;
623 1.1 christos }
624 1.1 christos }
625 1.1 christos
626 1.1 christos /* We can always read mapped registers. */
627 1.1 christos else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
628 1.1 christos return xtensa_register_read_masked (regcache, reg, buffer);
629 1.1 christos
630 1.1 christos /* Assume that we can read the register. */
631 1.1 christos return regcache_raw_read (regcache, regnum, buffer);
632 1.1 christos }
633 1.1 christos else
634 1.1 christos internal_error (__FILE__, __LINE__,
635 1.1 christos _("invalid register number %d"), regnum);
636 1.1 christos }
637 1.1 christos
638 1.1 christos
639 1.1 christos /* Write pseudo registers. */
640 1.1 christos
641 1.1 christos static void
642 1.1 christos xtensa_pseudo_register_write (struct gdbarch *gdbarch,
643 1.1 christos struct regcache *regcache,
644 1.1 christos int regnum,
645 1.1 christos const gdb_byte *buffer)
646 1.1 christos {
647 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
648 1.1 christos
649 1.1 christos DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
650 1.1 christos regnum, xtensa_register_name (gdbarch, regnum));
651 1.1 christos
652 1.1 christos /* Renumber register, if aliase a0..a15 on Windowed ABI. */
653 1.1 christos if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
654 1.1 christos && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
655 1.1 christos && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
656 1.1 christos {
657 1.1 christos gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
658 1.1 christos
659 1.1 christos regcache_raw_read (regcache,
660 1.1 christos gdbarch_tdep (gdbarch)->wb_regnum, buf);
661 1.1 christos regnum = arreg_number (gdbarch, regnum,
662 1.1 christos extract_unsigned_integer (buf, 4, byte_order));
663 1.1 christos }
664 1.1 christos
665 1.1 christos /* We can always write 'core' registers.
666 1.1 christos Note: We might have converted Ax->ARy. */
667 1.1 christos if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
668 1.1 christos regcache_raw_write (regcache, regnum, buffer);
669 1.1 christos
670 1.1 christos /* We have to find out how to deal with priveleged registers.
671 1.1 christos Let's treat them as pseudo-registers, but we cannot read/write them. */
672 1.1 christos
673 1.1 christos else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
674 1.1 christos {
675 1.1 christos return;
676 1.1 christos }
677 1.1 christos /* Pseudo registers. */
678 1.1 christos else if (regnum >= 0
679 1.1 christos && regnum < gdbarch_num_regs (gdbarch)
680 1.1 christos + gdbarch_num_pseudo_regs (gdbarch))
681 1.1 christos {
682 1.1 christos xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
683 1.1 christos xtensa_register_type_t type = reg->type;
684 1.1 christos int flags = gdbarch_tdep (gdbarch)->target_flags;
685 1.1 christos
686 1.1 christos /* On most targets, we cannot write registers
687 1.1 christos of type "Unknown" or "Unmapped". */
688 1.1 christos if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
689 1.1 christos {
690 1.1 christos if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
691 1.1 christos {
692 1.1 christos warning (_("cannot write register %s"),
693 1.1 christos xtensa_register_name (gdbarch, regnum));
694 1.1 christos return;
695 1.1 christos }
696 1.1 christos }
697 1.1 christos
698 1.1 christos /* Some targets cannot read TIE register files. */
699 1.1 christos else if (type == xtRegisterTypeTieRegfile)
700 1.1 christos {
701 1.1 christos /* Use 'store' to get register? */
702 1.1 christos if (flags & xtTargetFlagsUseFetchStore)
703 1.1 christos {
704 1.1 christos warning (_("cannot write register"));
705 1.1 christos return;
706 1.1 christos }
707 1.1 christos
708 1.1 christos /* On some targets (esp. simulators), we can always write
709 1.1 christos the register. */
710 1.1 christos else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
711 1.1 christos {
712 1.1 christos warning (_("cannot write register"));
713 1.1 christos return;
714 1.1 christos }
715 1.1 christos }
716 1.1 christos
717 1.1 christos /* We can always write mapped registers. */
718 1.1 christos else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
719 1.1 christos {
720 1.1 christos xtensa_register_write_masked (regcache, reg, buffer);
721 1.1 christos return;
722 1.1 christos }
723 1.1 christos
724 1.1 christos /* Assume that we can write the register. */
725 1.1 christos regcache_raw_write (regcache, regnum, buffer);
726 1.1 christos }
727 1.1 christos else
728 1.1 christos internal_error (__FILE__, __LINE__,
729 1.1 christos _("invalid register number %d"), regnum);
730 1.1 christos }
731 1.1 christos
732 1.1 christos static struct reggroup *xtensa_ar_reggroup;
733 1.1 christos static struct reggroup *xtensa_user_reggroup;
734 1.1 christos static struct reggroup *xtensa_vectra_reggroup;
735 1.1 christos static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
736 1.1 christos
737 1.1 christos static void
738 1.1 christos xtensa_init_reggroups (void)
739 1.1 christos {
740 1.1 christos int i;
741 1.1 christos char cpname[] = "cp0";
742 1.1 christos
743 1.1 christos xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
744 1.1 christos xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
745 1.1 christos xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
746 1.1 christos
747 1.1 christos for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
748 1.1 christos {
749 1.1 christos cpname[2] = '0' + i;
750 1.1 christos xtensa_cp[i] = reggroup_new (cpname, USER_REGGROUP);
751 1.1 christos }
752 1.1 christos }
753 1.1 christos
754 1.1 christos static void
755 1.1 christos xtensa_add_reggroups (struct gdbarch *gdbarch)
756 1.1 christos {
757 1.1 christos int i;
758 1.1 christos
759 1.1 christos /* Predefined groups. */
760 1.1 christos reggroup_add (gdbarch, all_reggroup);
761 1.1 christos reggroup_add (gdbarch, save_reggroup);
762 1.1 christos reggroup_add (gdbarch, restore_reggroup);
763 1.1 christos reggroup_add (gdbarch, system_reggroup);
764 1.1 christos reggroup_add (gdbarch, vector_reggroup);
765 1.1 christos reggroup_add (gdbarch, general_reggroup);
766 1.1 christos reggroup_add (gdbarch, float_reggroup);
767 1.1 christos
768 1.1 christos /* Xtensa-specific groups. */
769 1.1 christos reggroup_add (gdbarch, xtensa_ar_reggroup);
770 1.1 christos reggroup_add (gdbarch, xtensa_user_reggroup);
771 1.1 christos reggroup_add (gdbarch, xtensa_vectra_reggroup);
772 1.1 christos
773 1.1 christos for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
774 1.1 christos reggroup_add (gdbarch, xtensa_cp[i]);
775 1.1 christos }
776 1.1 christos
777 1.1 christos static int
778 1.1 christos xtensa_coprocessor_register_group (struct reggroup *group)
779 1.1 christos {
780 1.1 christos int i;
781 1.1 christos
782 1.1 christos for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
783 1.1 christos if (group == xtensa_cp[i])
784 1.1 christos return i;
785 1.1 christos
786 1.1 christos return -1;
787 1.1 christos }
788 1.1 christos
789 1.1 christos #define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
790 1.1 christos | XTENSA_REGISTER_FLAGS_WRITABLE \
791 1.1 christos | XTENSA_REGISTER_FLAGS_VOLATILE)
792 1.1 christos
793 1.1 christos #define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
794 1.1 christos | XTENSA_REGISTER_FLAGS_WRITABLE)
795 1.1 christos
796 1.1 christos static int
797 1.1 christos xtensa_register_reggroup_p (struct gdbarch *gdbarch,
798 1.1 christos int regnum,
799 1.1 christos struct reggroup *group)
800 1.1 christos {
801 1.1 christos xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
802 1.1 christos xtensa_register_type_t type = reg->type;
803 1.1 christos xtensa_register_group_t rg = reg->group;
804 1.1 christos int cp_number;
805 1.1 christos
806 1.1 christos if (group == save_reggroup)
807 1.1 christos /* Every single register should be included into the list of registers
808 1.1 christos to be watched for changes while using -data-list-changed-registers. */
809 1.1 christos return 1;
810 1.1 christos
811 1.1 christos /* First, skip registers that are not visible to this target
812 1.1 christos (unknown and unmapped registers when not using ISS). */
813 1.1 christos
814 1.1 christos if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
815 1.1 christos return 0;
816 1.1 christos if (group == all_reggroup)
817 1.1 christos return 1;
818 1.1 christos if (group == xtensa_ar_reggroup)
819 1.1 christos return rg & xtRegisterGroupAddrReg;
820 1.1 christos if (group == xtensa_user_reggroup)
821 1.1 christos return rg & xtRegisterGroupUser;
822 1.1 christos if (group == float_reggroup)
823 1.1 christos return rg & xtRegisterGroupFloat;
824 1.1 christos if (group == general_reggroup)
825 1.1 christos return rg & xtRegisterGroupGeneral;
826 1.1 christos if (group == system_reggroup)
827 1.1 christos return rg & xtRegisterGroupState;
828 1.1 christos if (group == vector_reggroup || group == xtensa_vectra_reggroup)
829 1.1 christos return rg & xtRegisterGroupVectra;
830 1.1 christos if (group == restore_reggroup)
831 1.1 christos return (regnum < gdbarch_num_regs (gdbarch)
832 1.1 christos && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
833 1.1 christos cp_number = xtensa_coprocessor_register_group (group);
834 1.1 christos if (cp_number >= 0)
835 1.1 christos return rg & (xtRegisterGroupCP0 << cp_number);
836 1.1 christos else
837 1.1 christos return 1;
838 1.1 christos }
839 1.1 christos
840 1.1 christos
841 1.1 christos /* Supply register REGNUM from the buffer specified by GREGS and LEN
842 1.1 christos in the general-purpose register set REGSET to register cache
843 1.1 christos REGCACHE. If REGNUM is -1 do this for all registers in REGSET. */
844 1.1 christos
845 1.1 christos static void
846 1.1 christos xtensa_supply_gregset (const struct regset *regset,
847 1.1 christos struct regcache *rc,
848 1.1 christos int regnum,
849 1.1 christos const void *gregs,
850 1.1 christos size_t len)
851 1.1 christos {
852 1.6 christos const xtensa_elf_gregset_t *regs = (const xtensa_elf_gregset_t *) gregs;
853 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (rc);
854 1.1 christos int i;
855 1.1 christos
856 1.1 christos DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);
857 1.1 christos
858 1.1 christos if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
859 1.1 christos regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) ®s->pc);
860 1.1 christos if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
861 1.1 christos regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) ®s->ps);
862 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
863 1.1 christos regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
864 1.1 christos (char *) ®s->windowbase);
865 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
866 1.1 christos regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
867 1.1 christos (char *) ®s->windowstart);
868 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
869 1.1 christos regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
870 1.1 christos (char *) ®s->lbeg);
871 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
872 1.1 christos regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
873 1.1 christos (char *) ®s->lend);
874 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
875 1.1 christos regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
876 1.1 christos (char *) ®s->lcount);
877 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
878 1.1 christos regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
879 1.1 christos (char *) ®s->sar);
880 1.1 christos if (regnum >=gdbarch_tdep (gdbarch)->ar_base
881 1.1 christos && regnum < gdbarch_tdep (gdbarch)->ar_base
882 1.1 christos + gdbarch_tdep (gdbarch)->num_aregs)
883 1.1 christos regcache_raw_supply (rc, regnum,
884 1.1 christos (char *) ®s->ar[regnum - gdbarch_tdep
885 1.1 christos (gdbarch)->ar_base]);
886 1.1 christos else if (regnum == -1)
887 1.1 christos {
888 1.1 christos for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
889 1.1 christos regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
890 1.1 christos (char *) ®s->ar[i]);
891 1.1 christos }
892 1.1 christos }
893 1.1 christos
894 1.1 christos
895 1.1 christos /* Xtensa register set. */
896 1.1 christos
897 1.1 christos static struct regset
898 1.1 christos xtensa_gregset =
899 1.1 christos {
900 1.1 christos NULL,
901 1.1 christos xtensa_supply_gregset
902 1.1 christos };
903 1.1 christos
904 1.1 christos
905 1.3 christos /* Iterate over supported core file register note sections. */
906 1.1 christos
907 1.3 christos static void
908 1.3 christos xtensa_iterate_over_regset_sections (struct gdbarch *gdbarch,
909 1.3 christos iterate_over_regset_sections_cb *cb,
910 1.3 christos void *cb_data,
911 1.3 christos const struct regcache *regcache)
912 1.3 christos {
913 1.3 christos DEBUGTRACE ("xtensa_iterate_over_regset_sections\n");
914 1.1 christos
915 1.3 christos cb (".reg", sizeof (xtensa_elf_gregset_t), &xtensa_gregset,
916 1.3 christos NULL, cb_data);
917 1.1 christos }
918 1.1 christos
919 1.1 christos
920 1.1 christos /* Handling frames. */
921 1.1 christos
922 1.1 christos /* Number of registers to save in case of Windowed ABI. */
923 1.1 christos #define XTENSA_NUM_SAVED_AREGS 12
924 1.1 christos
925 1.1 christos /* Frame cache part for Windowed ABI. */
926 1.1 christos typedef struct xtensa_windowed_frame_cache
927 1.1 christos {
928 1.1 christos int wb; /* WINDOWBASE of the previous frame. */
929 1.1 christos int callsize; /* Call size of this frame. */
930 1.1 christos int ws; /* WINDOWSTART of the previous frame. It keeps track of
931 1.1 christos life windows only. If there is no bit set for the
932 1.1 christos window, that means it had been already spilled
933 1.1 christos because of window overflow. */
934 1.1 christos
935 1.1 christos /* Addresses of spilled A-registers.
936 1.1 christos AREGS[i] == -1, if corresponding AR is alive. */
937 1.1 christos CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
938 1.1 christos } xtensa_windowed_frame_cache_t;
939 1.1 christos
940 1.1 christos /* Call0 ABI Definitions. */
941 1.1 christos
942 1.1 christos #define C0_MAXOPDS 3 /* Maximum number of operands for prologue
943 1.1 christos analysis. */
944 1.1 christos #define C0_NREGS 16 /* Number of A-registers to track. */
945 1.1 christos #define C0_CLESV 12 /* Callee-saved registers are here and up. */
946 1.1 christos #define C0_SP 1 /* Register used as SP. */
947 1.1 christos #define C0_FP 15 /* Register used as FP. */
948 1.1 christos #define C0_RA 0 /* Register used as return address. */
949 1.1 christos #define C0_ARGS 2 /* Register used as first arg/retval. */
950 1.1 christos #define C0_NARGS 6 /* Number of A-regs for args/retvals. */
951 1.1 christos
952 1.1 christos /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
953 1.1 christos A-register where the current content of the reg came from (in terms
954 1.1 christos of an original reg and a constant). Negative values of c0_rt[n].fp_reg
955 1.1 christos mean that the orignal content of the register was saved to the stack.
956 1.1 christos c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
957 1.1 christos know where SP will end up until the entire prologue has been analyzed. */
958 1.1 christos
959 1.1 christos #define C0_CONST -1 /* fr_reg value if register contains a constant. */
960 1.1 christos #define C0_INEXP -2 /* fr_reg value if inexpressible as reg + offset. */
961 1.1 christos #define C0_NOSTK -1 /* to_stk value if register has not been stored. */
962 1.1 christos
963 1.1 christos extern xtensa_isa xtensa_default_isa;
964 1.1 christos
965 1.1 christos typedef struct xtensa_c0reg
966 1.1 christos {
967 1.1 christos int fr_reg; /* original register from which register content
968 1.1 christos is derived, or C0_CONST, or C0_INEXP. */
969 1.1 christos int fr_ofs; /* constant offset from reg, or immediate value. */
970 1.1 christos int to_stk; /* offset from original SP to register (4-byte aligned),
971 1.1 christos or C0_NOSTK if register has not been saved. */
972 1.1 christos } xtensa_c0reg_t;
973 1.1 christos
974 1.1 christos /* Frame cache part for Call0 ABI. */
975 1.1 christos typedef struct xtensa_call0_frame_cache
976 1.1 christos {
977 1.1 christos int c0_frmsz; /* Stack frame size. */
978 1.1 christos int c0_hasfp; /* Current frame uses frame pointer. */
979 1.1 christos int fp_regnum; /* A-register used as FP. */
980 1.1 christos int c0_fp; /* Actual value of frame pointer. */
981 1.1 christos int c0_fpalign; /* Dinamic adjustment for the stack
982 1.1 christos pointer. It's an AND mask. Zero,
983 1.1 christos if alignment was not adjusted. */
984 1.1 christos int c0_old_sp; /* In case of dynamic adjustment, it is
985 1.1 christos a register holding unaligned sp.
986 1.1 christos C0_INEXP, when undefined. */
987 1.1 christos int c0_sp_ofs; /* If "c0_old_sp" was spilled it's a
988 1.1 christos stack offset. C0_NOSTK otherwise. */
989 1.1 christos
990 1.1 christos xtensa_c0reg_t c0_rt[C0_NREGS]; /* Register tracking information. */
991 1.1 christos } xtensa_call0_frame_cache_t;
992 1.1 christos
993 1.1 christos typedef struct xtensa_frame_cache
994 1.1 christos {
995 1.1 christos CORE_ADDR base; /* Stack pointer of this frame. */
996 1.1 christos CORE_ADDR pc; /* PC of this frame at the function entry point. */
997 1.1 christos CORE_ADDR ra; /* The raw return address of this frame. */
998 1.1 christos CORE_ADDR ps; /* The PS register of the previous (older) frame. */
999 1.1 christos CORE_ADDR prev_sp; /* Stack Pointer of the previous (older) frame. */
1000 1.1 christos int call0; /* It's a call0 framework (else windowed). */
1001 1.1 christos union
1002 1.1 christos {
1003 1.1 christos xtensa_windowed_frame_cache_t wd; /* call0 == false. */
1004 1.1 christos xtensa_call0_frame_cache_t c0; /* call0 == true. */
1005 1.1 christos };
1006 1.1 christos } xtensa_frame_cache_t;
1007 1.1 christos
1008 1.1 christos
1009 1.1 christos static struct xtensa_frame_cache *
1010 1.1 christos xtensa_alloc_frame_cache (int windowed)
1011 1.1 christos {
1012 1.1 christos xtensa_frame_cache_t *cache;
1013 1.1 christos int i;
1014 1.1 christos
1015 1.1 christos DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
1016 1.1 christos
1017 1.1 christos cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
1018 1.1 christos
1019 1.1 christos cache->base = 0;
1020 1.1 christos cache->pc = 0;
1021 1.1 christos cache->ra = 0;
1022 1.1 christos cache->ps = 0;
1023 1.1 christos cache->prev_sp = 0;
1024 1.1 christos cache->call0 = !windowed;
1025 1.1 christos if (cache->call0)
1026 1.1 christos {
1027 1.1 christos cache->c0.c0_frmsz = -1;
1028 1.1 christos cache->c0.c0_hasfp = 0;
1029 1.1 christos cache->c0.fp_regnum = -1;
1030 1.1 christos cache->c0.c0_fp = -1;
1031 1.1 christos cache->c0.c0_fpalign = 0;
1032 1.1 christos cache->c0.c0_old_sp = C0_INEXP;
1033 1.1 christos cache->c0.c0_sp_ofs = C0_NOSTK;
1034 1.1 christos
1035 1.1 christos for (i = 0; i < C0_NREGS; i++)
1036 1.1 christos {
1037 1.1 christos cache->c0.c0_rt[i].fr_reg = i;
1038 1.1 christos cache->c0.c0_rt[i].fr_ofs = 0;
1039 1.1 christos cache->c0.c0_rt[i].to_stk = C0_NOSTK;
1040 1.1 christos }
1041 1.1 christos }
1042 1.1 christos else
1043 1.1 christos {
1044 1.1 christos cache->wd.wb = 0;
1045 1.1 christos cache->wd.ws = 0;
1046 1.1 christos cache->wd.callsize = -1;
1047 1.1 christos
1048 1.1 christos for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
1049 1.1 christos cache->wd.aregs[i] = -1;
1050 1.1 christos }
1051 1.1 christos return cache;
1052 1.1 christos }
1053 1.1 christos
1054 1.1 christos
1055 1.1 christos static CORE_ADDR
1056 1.1 christos xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
1057 1.1 christos {
1058 1.1 christos return address & ~15;
1059 1.1 christos }
1060 1.1 christos
1061 1.1 christos
1062 1.1 christos static CORE_ADDR
1063 1.1 christos xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1064 1.1 christos {
1065 1.1 christos gdb_byte buf[8];
1066 1.1 christos CORE_ADDR pc;
1067 1.1 christos
1068 1.1 christos DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n",
1069 1.1 christos host_address_to_string (next_frame));
1070 1.1 christos
1071 1.1 christos frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1072 1.1 christos pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1073 1.1 christos
1074 1.1 christos DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
1075 1.1 christos
1076 1.1 christos return pc;
1077 1.1 christos }
1078 1.1 christos
1079 1.1 christos
1080 1.1 christos static struct frame_id
1081 1.1 christos xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1082 1.1 christos {
1083 1.1 christos CORE_ADDR pc, fp;
1084 1.1 christos
1085 1.1 christos /* THIS-FRAME is a dummy frame. Return a frame ID of that frame. */
1086 1.1 christos
1087 1.1 christos pc = get_frame_pc (this_frame);
1088 1.1 christos fp = get_frame_register_unsigned
1089 1.1 christos (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1090 1.1 christos
1091 1.1 christos /* Make dummy frame ID unique by adding a constant. */
1092 1.1 christos return frame_id_build (fp + SP_ALIGNMENT, pc);
1093 1.1 christos }
1094 1.1 christos
1095 1.1 christos /* Returns true, if instruction to execute next is unique to Xtensa Window
1096 1.1 christos Interrupt Handlers. It can only be one of L32E, S32E, RFWO, or RFWU. */
1097 1.1 christos
1098 1.1 christos static int
1099 1.1 christos xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
1100 1.1 christos {
1101 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1102 1.1 christos unsigned int insn = read_memory_integer (pc, 4, byte_order);
1103 1.1 christos unsigned int code;
1104 1.1 christos
1105 1.1 christos if (byte_order == BFD_ENDIAN_BIG)
1106 1.1 christos {
1107 1.1 christos /* Check, if this is L32E or S32E. */
1108 1.1 christos code = insn & 0xf000ff00;
1109 1.1 christos if ((code == 0x00009000) || (code == 0x00009400))
1110 1.1 christos return 1;
1111 1.1 christos /* Check, if this is RFWU or RFWO. */
1112 1.1 christos code = insn & 0xffffff00;
1113 1.1 christos return ((code == 0x00430000) || (code == 0x00530000));
1114 1.1 christos }
1115 1.1 christos else
1116 1.1 christos {
1117 1.1 christos /* Check, if this is L32E or S32E. */
1118 1.1 christos code = insn & 0x00ff000f;
1119 1.1 christos if ((code == 0x090000) || (code == 0x490000))
1120 1.1 christos return 1;
1121 1.1 christos /* Check, if this is RFWU or RFWO. */
1122 1.1 christos code = insn & 0x00ffffff;
1123 1.1 christos return ((code == 0x00003400) || (code == 0x00003500));
1124 1.1 christos }
1125 1.1 christos }
1126 1.1 christos
1127 1.1 christos /* Returns the best guess about which register is a frame pointer
1128 1.1 christos for the function containing CURRENT_PC. */
1129 1.1 christos
1130 1.1 christos #define XTENSA_ISA_BSZ 32 /* Instruction buffer size. */
1131 1.1 christos #define XTENSA_ISA_BADPC ((CORE_ADDR)0) /* Bad PC value. */
1132 1.1 christos
1133 1.1 christos static unsigned int
1134 1.1 christos xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
1135 1.1 christos {
1136 1.1 christos #define RETURN_FP goto done
1137 1.1 christos
1138 1.1 christos unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
1139 1.1 christos CORE_ADDR start_addr;
1140 1.1 christos xtensa_isa isa;
1141 1.1 christos xtensa_insnbuf ins, slot;
1142 1.1 christos gdb_byte ibuf[XTENSA_ISA_BSZ];
1143 1.1 christos CORE_ADDR ia, bt, ba;
1144 1.1 christos xtensa_format ifmt;
1145 1.1 christos int ilen, islots, is;
1146 1.1 christos xtensa_opcode opc;
1147 1.1 christos const char *opcname;
1148 1.1 christos
1149 1.1 christos find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
1150 1.1 christos if (start_addr == 0)
1151 1.1 christos return fp_regnum;
1152 1.1 christos
1153 1.1 christos if (!xtensa_default_isa)
1154 1.1 christos xtensa_default_isa = xtensa_isa_init (0, 0);
1155 1.1 christos isa = xtensa_default_isa;
1156 1.1 christos gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
1157 1.1 christos ins = xtensa_insnbuf_alloc (isa);
1158 1.1 christos slot = xtensa_insnbuf_alloc (isa);
1159 1.1 christos ba = 0;
1160 1.1 christos
1161 1.1 christos for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
1162 1.1 christos {
1163 1.1 christos if (ia + xtensa_isa_maxlength (isa) > bt)
1164 1.1 christos {
1165 1.1 christos ba = ia;
1166 1.1 christos bt = (ba + XTENSA_ISA_BSZ) < current_pc
1167 1.1 christos ? ba + XTENSA_ISA_BSZ : current_pc;
1168 1.1 christos if (target_read_memory (ba, ibuf, bt - ba) != 0)
1169 1.1 christos RETURN_FP;
1170 1.1 christos }
1171 1.1 christos
1172 1.1 christos xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
1173 1.1 christos ifmt = xtensa_format_decode (isa, ins);
1174 1.1 christos if (ifmt == XTENSA_UNDEFINED)
1175 1.1 christos RETURN_FP;
1176 1.1 christos ilen = xtensa_format_length (isa, ifmt);
1177 1.1 christos if (ilen == XTENSA_UNDEFINED)
1178 1.1 christos RETURN_FP;
1179 1.1 christos islots = xtensa_format_num_slots (isa, ifmt);
1180 1.1 christos if (islots == XTENSA_UNDEFINED)
1181 1.1 christos RETURN_FP;
1182 1.1 christos
1183 1.1 christos for (is = 0; is < islots; ++is)
1184 1.1 christos {
1185 1.1 christos if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
1186 1.1 christos RETURN_FP;
1187 1.1 christos
1188 1.1 christos opc = xtensa_opcode_decode (isa, ifmt, is, slot);
1189 1.1 christos if (opc == XTENSA_UNDEFINED)
1190 1.1 christos RETURN_FP;
1191 1.1 christos
1192 1.1 christos opcname = xtensa_opcode_name (isa, opc);
1193 1.1 christos
1194 1.1 christos if (strcasecmp (opcname, "mov.n") == 0
1195 1.1 christos || strcasecmp (opcname, "or") == 0)
1196 1.1 christos {
1197 1.1 christos unsigned int register_operand;
1198 1.1 christos
1199 1.1 christos /* Possible candidate for setting frame pointer
1200 1.1 christos from A1. This is what we are looking for. */
1201 1.1 christos
1202 1.1 christos if (xtensa_operand_get_field (isa, opc, 1, ifmt,
1203 1.1 christos is, slot, ®ister_operand) != 0)
1204 1.1 christos RETURN_FP;
1205 1.1 christos if (xtensa_operand_decode (isa, opc, 1, ®ister_operand) != 0)
1206 1.1 christos RETURN_FP;
1207 1.1 christos if (register_operand == 1) /* Mov{.n} FP A1. */
1208 1.1 christos {
1209 1.1 christos if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
1210 1.1 christos ®ister_operand) != 0)
1211 1.1 christos RETURN_FP;
1212 1.1 christos if (xtensa_operand_decode (isa, opc, 0,
1213 1.1 christos ®ister_operand) != 0)
1214 1.1 christos RETURN_FP;
1215 1.1 christos
1216 1.1 christos fp_regnum
1217 1.1 christos = gdbarch_tdep (gdbarch)->a0_base + register_operand;
1218 1.1 christos RETURN_FP;
1219 1.1 christos }
1220 1.1 christos }
1221 1.1 christos
1222 1.1 christos if (
1223 1.1 christos /* We have problems decoding the memory. */
1224 1.1 christos opcname == NULL
1225 1.1 christos || strcasecmp (opcname, "ill") == 0
1226 1.1 christos || strcasecmp (opcname, "ill.n") == 0
1227 1.1 christos /* Hit planted breakpoint. */
1228 1.1 christos || strcasecmp (opcname, "break") == 0
1229 1.1 christos || strcasecmp (opcname, "break.n") == 0
1230 1.1 christos /* Flow control instructions finish prologue. */
1231 1.1 christos || xtensa_opcode_is_branch (isa, opc) > 0
1232 1.1 christos || xtensa_opcode_is_jump (isa, opc) > 0
1233 1.1 christos || xtensa_opcode_is_loop (isa, opc) > 0
1234 1.1 christos || xtensa_opcode_is_call (isa, opc) > 0
1235 1.1 christos || strcasecmp (opcname, "simcall") == 0
1236 1.1 christos || strcasecmp (opcname, "syscall") == 0)
1237 1.1 christos /* Can not continue analysis. */
1238 1.1 christos RETURN_FP;
1239 1.1 christos }
1240 1.1 christos }
1241 1.1 christos done:
1242 1.1 christos xtensa_insnbuf_free(isa, slot);
1243 1.1 christos xtensa_insnbuf_free(isa, ins);
1244 1.1 christos return fp_regnum;
1245 1.1 christos }
1246 1.1 christos
1247 1.1 christos /* The key values to identify the frame using "cache" are
1248 1.1 christos
1249 1.1 christos cache->base = SP (or best guess about FP) of this frame;
1250 1.1 christos cache->pc = entry-PC (entry point of the frame function);
1251 1.1 christos cache->prev_sp = SP of the previous frame. */
1252 1.1 christos
1253 1.1 christos static void
1254 1.1 christos call0_frame_cache (struct frame_info *this_frame,
1255 1.1 christos xtensa_frame_cache_t *cache, CORE_ADDR pc);
1256 1.1 christos
1257 1.1 christos static void
1258 1.1 christos xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
1259 1.1 christos xtensa_frame_cache_t *cache,
1260 1.1 christos CORE_ADDR pc);
1261 1.1 christos
1262 1.1 christos static struct xtensa_frame_cache *
1263 1.1 christos xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
1264 1.1 christos {
1265 1.1 christos xtensa_frame_cache_t *cache;
1266 1.1 christos CORE_ADDR ra, wb, ws, pc, sp, ps;
1267 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
1268 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1269 1.1 christos unsigned int fp_regnum;
1270 1.1 christos int windowed, ps_regnum;
1271 1.1 christos
1272 1.1 christos if (*this_cache)
1273 1.6 christos return (struct xtensa_frame_cache *) *this_cache;
1274 1.1 christos
1275 1.1 christos pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
1276 1.1 christos ps_regnum = gdbarch_ps_regnum (gdbarch);
1277 1.1 christos ps = (ps_regnum >= 0
1278 1.1 christos ? get_frame_register_unsigned (this_frame, ps_regnum) : TX_PS);
1279 1.1 christos
1280 1.1 christos windowed = windowing_enabled (gdbarch, ps);
1281 1.1 christos
1282 1.1 christos /* Get pristine xtensa-frame. */
1283 1.1 christos cache = xtensa_alloc_frame_cache (windowed);
1284 1.1 christos *this_cache = cache;
1285 1.1 christos
1286 1.1 christos if (windowed)
1287 1.1 christos {
1288 1.6 christos LONGEST op1;
1289 1.1 christos
1290 1.1 christos /* Get WINDOWBASE, WINDOWSTART, and PS registers. */
1291 1.1 christos wb = get_frame_register_unsigned (this_frame,
1292 1.1 christos gdbarch_tdep (gdbarch)->wb_regnum);
1293 1.1 christos ws = get_frame_register_unsigned (this_frame,
1294 1.1 christos gdbarch_tdep (gdbarch)->ws_regnum);
1295 1.1 christos
1296 1.6 christos if (safe_read_memory_integer (pc, 1, byte_order, &op1)
1297 1.6 christos && XTENSA_IS_ENTRY (gdbarch, op1))
1298 1.1 christos {
1299 1.1 christos int callinc = CALLINC (ps);
1300 1.1 christos ra = get_frame_register_unsigned
1301 1.1 christos (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
1302 1.1 christos
1303 1.1 christos /* ENTRY hasn't been executed yet, therefore callsize is still 0. */
1304 1.1 christos cache->wd.callsize = 0;
1305 1.1 christos cache->wd.wb = wb;
1306 1.1 christos cache->wd.ws = ws;
1307 1.1 christos cache->prev_sp = get_frame_register_unsigned
1308 1.1 christos (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1309 1.1 christos
1310 1.1 christos /* This only can be the outermost frame since we are
1311 1.1 christos just about to execute ENTRY. SP hasn't been set yet.
1312 1.1 christos We can assume any frame size, because it does not
1313 1.1 christos matter, and, let's fake frame base in cache. */
1314 1.1 christos cache->base = cache->prev_sp - 16;
1315 1.1 christos
1316 1.1 christos cache->pc = pc;
1317 1.1 christos cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1318 1.1 christos cache->ps = (ps & ~PS_CALLINC_MASK)
1319 1.1 christos | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1320 1.1 christos
1321 1.1 christos return cache;
1322 1.1 christos }
1323 1.1 christos else
1324 1.1 christos {
1325 1.1 christos fp_regnum = xtensa_scan_prologue (gdbarch, pc);
1326 1.1 christos ra = get_frame_register_unsigned (this_frame,
1327 1.1 christos gdbarch_tdep (gdbarch)->a0_base);
1328 1.1 christos cache->wd.callsize = WINSIZE (ra);
1329 1.1 christos cache->wd.wb = (wb - cache->wd.callsize / 4)
1330 1.1 christos & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
1331 1.1 christos cache->wd.ws = ws & ~(1 << wb);
1332 1.1 christos
1333 1.1 christos cache->pc = get_frame_func (this_frame);
1334 1.1 christos cache->ra = (pc & 0xc0000000) | (ra & 0x3fffffff);
1335 1.1 christos cache->ps = (ps & ~PS_CALLINC_MASK)
1336 1.1 christos | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1337 1.1 christos }
1338 1.1 christos
1339 1.1 christos if (cache->wd.ws == 0)
1340 1.1 christos {
1341 1.1 christos int i;
1342 1.1 christos
1343 1.1 christos /* Set A0...A3. */
1344 1.1 christos sp = get_frame_register_unsigned
1345 1.1 christos (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
1346 1.1 christos
1347 1.1 christos for (i = 0; i < 4; i++, sp += 4)
1348 1.1 christos {
1349 1.1 christos cache->wd.aregs[i] = sp;
1350 1.1 christos }
1351 1.1 christos
1352 1.1 christos if (cache->wd.callsize > 4)
1353 1.1 christos {
1354 1.1 christos /* Set A4...A7/A11. */
1355 1.1 christos /* Get the SP of the frame previous to the previous one.
1356 1.1 christos To achieve this, we have to dereference SP twice. */
1357 1.1 christos sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
1358 1.1 christos sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
1359 1.1 christos sp -= cache->wd.callsize * 4;
1360 1.1 christos
1361 1.1 christos for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
1362 1.1 christos {
1363 1.1 christos cache->wd.aregs[i] = sp;
1364 1.1 christos }
1365 1.1 christos }
1366 1.1 christos }
1367 1.1 christos
1368 1.1 christos if ((cache->prev_sp == 0) && ( ra != 0 ))
1369 1.1 christos /* If RA is equal to 0 this frame is an outermost frame. Leave
1370 1.1 christos cache->prev_sp unchanged marking the boundary of the frame stack. */
1371 1.1 christos {
1372 1.1 christos if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
1373 1.1 christos {
1374 1.1 christos /* Register window overflow already happened.
1375 1.1 christos We can read caller's SP from the proper spill loction. */
1376 1.1 christos sp = get_frame_register_unsigned
1377 1.1 christos (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
1378 1.1 christos cache->prev_sp = read_memory_integer (sp - 12, 4, byte_order);
1379 1.1 christos }
1380 1.1 christos else
1381 1.1 christos {
1382 1.1 christos /* Read caller's frame SP directly from the previous window. */
1383 1.1 christos int regnum = arreg_number
1384 1.1 christos (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
1385 1.1 christos cache->wd.wb);
1386 1.1 christos
1387 1.1 christos cache->prev_sp = xtensa_read_register (regnum);
1388 1.1 christos }
1389 1.1 christos }
1390 1.1 christos }
1391 1.1 christos else if (xtensa_window_interrupt_insn (gdbarch, pc))
1392 1.1 christos {
1393 1.1 christos /* Execution stopped inside Xtensa Window Interrupt Handler. */
1394 1.1 christos
1395 1.1 christos xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
1396 1.1 christos /* Everything was set already, including cache->base. */
1397 1.1 christos return cache;
1398 1.1 christos }
1399 1.1 christos else /* Call0 framework. */
1400 1.1 christos {
1401 1.1 christos call0_frame_cache (this_frame, cache, pc);
1402 1.1 christos fp_regnum = cache->c0.fp_regnum;
1403 1.1 christos }
1404 1.1 christos
1405 1.1 christos cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
1406 1.1 christos
1407 1.1 christos return cache;
1408 1.1 christos }
1409 1.1 christos
1410 1.1 christos static int xtensa_session_once_reported = 1;
1411 1.1 christos
1412 1.1 christos /* Report a problem with prologue analysis while doing backtracing.
1413 1.1 christos But, do it only once to avoid annoyng repeated messages. */
1414 1.1 christos
1415 1.1 christos static void
1416 1.1 christos warning_once (void)
1417 1.1 christos {
1418 1.1 christos if (xtensa_session_once_reported == 0)
1419 1.1 christos warning (_("\
1420 1.1 christos \nUnrecognised function prologue. Stack trace cannot be resolved. \
1421 1.1 christos This message will not be repeated in this session.\n"));
1422 1.1 christos
1423 1.1 christos xtensa_session_once_reported = 1;
1424 1.1 christos }
1425 1.1 christos
1426 1.1 christos
1427 1.1 christos static void
1428 1.1 christos xtensa_frame_this_id (struct frame_info *this_frame,
1429 1.1 christos void **this_cache,
1430 1.1 christos struct frame_id *this_id)
1431 1.1 christos {
1432 1.1 christos struct xtensa_frame_cache *cache =
1433 1.1 christos xtensa_frame_cache (this_frame, this_cache);
1434 1.1 christos
1435 1.1 christos if (cache->prev_sp == 0)
1436 1.1 christos return;
1437 1.1 christos
1438 1.1 christos (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
1439 1.1 christos }
1440 1.1 christos
1441 1.1 christos static struct value *
1442 1.1 christos xtensa_frame_prev_register (struct frame_info *this_frame,
1443 1.1 christos void **this_cache,
1444 1.1 christos int regnum)
1445 1.1 christos {
1446 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
1447 1.1 christos struct xtensa_frame_cache *cache;
1448 1.1 christos ULONGEST saved_reg = 0;
1449 1.1 christos int done = 1;
1450 1.1 christos
1451 1.1 christos if (*this_cache == NULL)
1452 1.1 christos *this_cache = xtensa_frame_cache (this_frame, this_cache);
1453 1.6 christos cache = (struct xtensa_frame_cache *) *this_cache;
1454 1.1 christos
1455 1.1 christos if (regnum ==gdbarch_pc_regnum (gdbarch))
1456 1.1 christos saved_reg = cache->ra;
1457 1.1 christos else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
1458 1.1 christos saved_reg = cache->prev_sp;
1459 1.1 christos else if (!cache->call0)
1460 1.1 christos {
1461 1.1 christos if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
1462 1.1 christos saved_reg = cache->wd.ws;
1463 1.1 christos else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
1464 1.1 christos saved_reg = cache->wd.wb;
1465 1.1 christos else if (regnum == gdbarch_ps_regnum (gdbarch))
1466 1.1 christos saved_reg = cache->ps;
1467 1.1 christos else
1468 1.1 christos done = 0;
1469 1.1 christos }
1470 1.1 christos else
1471 1.1 christos done = 0;
1472 1.1 christos
1473 1.1 christos if (done)
1474 1.1 christos return frame_unwind_got_constant (this_frame, regnum, saved_reg);
1475 1.1 christos
1476 1.1 christos if (!cache->call0) /* Windowed ABI. */
1477 1.1 christos {
1478 1.1 christos /* Convert A-register numbers to AR-register numbers,
1479 1.1 christos if we deal with A-register. */
1480 1.1 christos if (regnum >= gdbarch_tdep (gdbarch)->a0_base
1481 1.1 christos && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
1482 1.1 christos regnum = arreg_number (gdbarch, regnum, cache->wd.wb);
1483 1.1 christos
1484 1.1 christos /* Check, if we deal with AR-register saved on stack. */
1485 1.1 christos if (regnum >= gdbarch_tdep (gdbarch)->ar_base
1486 1.1 christos && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1487 1.1 christos + gdbarch_tdep (gdbarch)->num_aregs))
1488 1.1 christos {
1489 1.1 christos int areg = areg_number (gdbarch, regnum, cache->wd.wb);
1490 1.1 christos
1491 1.1 christos if (areg >= 0
1492 1.1 christos && areg < XTENSA_NUM_SAVED_AREGS
1493 1.1 christos && cache->wd.aregs[areg] != -1)
1494 1.1 christos return frame_unwind_got_memory (this_frame, regnum,
1495 1.1 christos cache->wd.aregs[areg]);
1496 1.1 christos }
1497 1.1 christos }
1498 1.1 christos else /* Call0 ABI. */
1499 1.1 christos {
1500 1.1 christos int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
1501 1.1 christos && regnum <= (gdbarch_tdep (gdbarch)->ar_base
1502 1.1 christos + C0_NREGS))
1503 1.1 christos ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
1504 1.1 christos
1505 1.1 christos if (reg < C0_NREGS)
1506 1.1 christos {
1507 1.1 christos CORE_ADDR spe;
1508 1.1 christos int stkofs;
1509 1.1 christos
1510 1.1 christos /* If register was saved in the prologue, retrieve it. */
1511 1.1 christos stkofs = cache->c0.c0_rt[reg].to_stk;
1512 1.1 christos if (stkofs != C0_NOSTK)
1513 1.1 christos {
1514 1.1 christos /* Determine SP on entry based on FP. */
1515 1.1 christos spe = cache->c0.c0_fp
1516 1.1 christos - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1517 1.1 christos
1518 1.1 christos return frame_unwind_got_memory (this_frame, regnum,
1519 1.1 christos spe + stkofs);
1520 1.1 christos }
1521 1.1 christos }
1522 1.1 christos }
1523 1.1 christos
1524 1.1 christos /* All other registers have been either saved to
1525 1.1 christos the stack or are still alive in the processor. */
1526 1.1 christos
1527 1.1 christos return frame_unwind_got_register (this_frame, regnum, regnum);
1528 1.1 christos }
1529 1.1 christos
1530 1.1 christos
1531 1.1 christos static const struct frame_unwind
1532 1.1 christos xtensa_unwind =
1533 1.1 christos {
1534 1.1 christos NORMAL_FRAME,
1535 1.1 christos default_frame_unwind_stop_reason,
1536 1.1 christos xtensa_frame_this_id,
1537 1.1 christos xtensa_frame_prev_register,
1538 1.1 christos NULL,
1539 1.1 christos default_frame_sniffer
1540 1.1 christos };
1541 1.1 christos
1542 1.1 christos static CORE_ADDR
1543 1.1 christos xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
1544 1.1 christos {
1545 1.1 christos struct xtensa_frame_cache *cache =
1546 1.1 christos xtensa_frame_cache (this_frame, this_cache);
1547 1.1 christos
1548 1.1 christos return cache->base;
1549 1.1 christos }
1550 1.1 christos
1551 1.1 christos static const struct frame_base
1552 1.1 christos xtensa_frame_base =
1553 1.1 christos {
1554 1.1 christos &xtensa_unwind,
1555 1.1 christos xtensa_frame_base_address,
1556 1.1 christos xtensa_frame_base_address,
1557 1.1 christos xtensa_frame_base_address
1558 1.1 christos };
1559 1.1 christos
1560 1.1 christos
1561 1.1 christos static void
1562 1.1 christos xtensa_extract_return_value (struct type *type,
1563 1.1 christos struct regcache *regcache,
1564 1.1 christos void *dst)
1565 1.1 christos {
1566 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
1567 1.6 christos bfd_byte *valbuf = (bfd_byte *) dst;
1568 1.1 christos int len = TYPE_LENGTH (type);
1569 1.1 christos ULONGEST pc, wb;
1570 1.1 christos int callsize, areg;
1571 1.1 christos int offset = 0;
1572 1.1 christos
1573 1.1 christos DEBUGTRACE ("xtensa_extract_return_value (...)\n");
1574 1.1 christos
1575 1.1 christos gdb_assert(len > 0);
1576 1.1 christos
1577 1.1 christos if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1578 1.1 christos {
1579 1.1 christos /* First, we have to find the caller window in the register file. */
1580 1.1 christos regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1581 1.1 christos callsize = extract_call_winsize (gdbarch, pc);
1582 1.1 christos
1583 1.1 christos /* On Xtensa, we can return up to 4 words (or 2 for call12). */
1584 1.1 christos if (len > (callsize > 8 ? 8 : 16))
1585 1.1 christos internal_error (__FILE__, __LINE__,
1586 1.1 christos _("cannot extract return value of %d bytes long"),
1587 1.1 christos len);
1588 1.1 christos
1589 1.1 christos /* Get the register offset of the return
1590 1.1 christos register (A2) in the caller window. */
1591 1.1 christos regcache_raw_read_unsigned
1592 1.1 christos (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1593 1.1 christos areg = arreg_number (gdbarch,
1594 1.1 christos gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1595 1.1 christos }
1596 1.1 christos else
1597 1.1 christos {
1598 1.1 christos /* No windowing hardware - Call0 ABI. */
1599 1.1 christos areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1600 1.1 christos }
1601 1.1 christos
1602 1.1 christos DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
1603 1.1 christos
1604 1.1 christos if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1605 1.1 christos offset = 4 - len;
1606 1.1 christos
1607 1.1 christos for (; len > 0; len -= 4, areg++, valbuf += 4)
1608 1.1 christos {
1609 1.1 christos if (len < 4)
1610 1.1 christos regcache_raw_read_part (regcache, areg, offset, len, valbuf);
1611 1.1 christos else
1612 1.1 christos regcache_raw_read (regcache, areg, valbuf);
1613 1.1 christos }
1614 1.1 christos }
1615 1.1 christos
1616 1.1 christos
1617 1.1 christos static void
1618 1.1 christos xtensa_store_return_value (struct type *type,
1619 1.1 christos struct regcache *regcache,
1620 1.1 christos const void *dst)
1621 1.1 christos {
1622 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
1623 1.6 christos const bfd_byte *valbuf = (const bfd_byte *) dst;
1624 1.1 christos unsigned int areg;
1625 1.1 christos ULONGEST pc, wb;
1626 1.1 christos int callsize;
1627 1.1 christos int len = TYPE_LENGTH (type);
1628 1.1 christos int offset = 0;
1629 1.1 christos
1630 1.1 christos DEBUGTRACE ("xtensa_store_return_value (...)\n");
1631 1.1 christos
1632 1.1 christos if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1633 1.1 christos {
1634 1.1 christos regcache_raw_read_unsigned
1635 1.1 christos (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
1636 1.1 christos regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
1637 1.1 christos callsize = extract_call_winsize (gdbarch, pc);
1638 1.1 christos
1639 1.1 christos if (len > (callsize > 8 ? 8 : 16))
1640 1.1 christos internal_error (__FILE__, __LINE__,
1641 1.1 christos _("unimplemented for this length: %d"),
1642 1.1 christos TYPE_LENGTH (type));
1643 1.1 christos areg = arreg_number (gdbarch,
1644 1.1 christos gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
1645 1.1 christos
1646 1.1 christos DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
1647 1.1 christos callsize, (int) wb);
1648 1.1 christos }
1649 1.1 christos else
1650 1.1 christos {
1651 1.1 christos areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
1652 1.1 christos }
1653 1.1 christos
1654 1.1 christos if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1655 1.1 christos offset = 4 - len;
1656 1.1 christos
1657 1.1 christos for (; len > 0; len -= 4, areg++, valbuf += 4)
1658 1.1 christos {
1659 1.1 christos if (len < 4)
1660 1.1 christos regcache_raw_write_part (regcache, areg, offset, len, valbuf);
1661 1.1 christos else
1662 1.1 christos regcache_raw_write (regcache, areg, valbuf);
1663 1.1 christos }
1664 1.1 christos }
1665 1.1 christos
1666 1.1 christos
1667 1.1 christos static enum return_value_convention
1668 1.1 christos xtensa_return_value (struct gdbarch *gdbarch,
1669 1.1 christos struct value *function,
1670 1.1 christos struct type *valtype,
1671 1.1 christos struct regcache *regcache,
1672 1.1 christos gdb_byte *readbuf,
1673 1.1 christos const gdb_byte *writebuf)
1674 1.1 christos {
1675 1.1 christos /* Structures up to 16 bytes are returned in registers. */
1676 1.1 christos
1677 1.1 christos int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1678 1.1 christos || TYPE_CODE (valtype) == TYPE_CODE_UNION
1679 1.1 christos || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1680 1.1 christos && TYPE_LENGTH (valtype) > 16);
1681 1.1 christos
1682 1.1 christos if (struct_return)
1683 1.1 christos return RETURN_VALUE_STRUCT_CONVENTION;
1684 1.1 christos
1685 1.1 christos DEBUGTRACE ("xtensa_return_value(...)\n");
1686 1.1 christos
1687 1.1 christos if (writebuf != NULL)
1688 1.1 christos {
1689 1.1 christos xtensa_store_return_value (valtype, regcache, writebuf);
1690 1.1 christos }
1691 1.1 christos
1692 1.1 christos if (readbuf != NULL)
1693 1.1 christos {
1694 1.1 christos gdb_assert (!struct_return);
1695 1.1 christos xtensa_extract_return_value (valtype, regcache, readbuf);
1696 1.1 christos }
1697 1.1 christos return RETURN_VALUE_REGISTER_CONVENTION;
1698 1.1 christos }
1699 1.1 christos
1700 1.1 christos
1701 1.1 christos /* DUMMY FRAME */
1702 1.1 christos
1703 1.1 christos static CORE_ADDR
1704 1.1 christos xtensa_push_dummy_call (struct gdbarch *gdbarch,
1705 1.1 christos struct value *function,
1706 1.1 christos struct regcache *regcache,
1707 1.1 christos CORE_ADDR bp_addr,
1708 1.1 christos int nargs,
1709 1.1 christos struct value **args,
1710 1.1 christos CORE_ADDR sp,
1711 1.1 christos int struct_return,
1712 1.1 christos CORE_ADDR struct_addr)
1713 1.1 christos {
1714 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1715 1.1 christos int i;
1716 1.1 christos int size, onstack_size;
1717 1.1 christos gdb_byte *buf = (gdb_byte *) alloca (16);
1718 1.1 christos CORE_ADDR ra, ps;
1719 1.1 christos struct argument_info
1720 1.1 christos {
1721 1.1 christos const bfd_byte *contents;
1722 1.1 christos int length;
1723 1.1 christos int onstack; /* onstack == 0 => in reg */
1724 1.1 christos int align; /* alignment */
1725 1.1 christos union
1726 1.1 christos {
1727 1.1 christos int offset; /* stack offset if on stack. */
1728 1.1 christos int regno; /* regno if in register. */
1729 1.1 christos } u;
1730 1.1 christos };
1731 1.1 christos
1732 1.1 christos struct argument_info *arg_info =
1733 1.1 christos (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
1734 1.1 christos
1735 1.1 christos CORE_ADDR osp = sp;
1736 1.1 christos
1737 1.1 christos DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
1738 1.1 christos
1739 1.1 christos if (xtensa_debug_level > 3)
1740 1.1 christos {
1741 1.1 christos int i;
1742 1.1 christos DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
1743 1.1 christos DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
1744 1.1 christos "struct_addr=0x%x\n",
1745 1.1 christos (int) sp, (int) struct_return, (int) struct_addr);
1746 1.1 christos
1747 1.1 christos for (i = 0; i < nargs; i++)
1748 1.1 christos {
1749 1.1 christos struct value *arg = args[i];
1750 1.1 christos struct type *arg_type = check_typedef (value_type (arg));
1751 1.1 christos fprintf_unfiltered (gdb_stdlog, "%2d: %s %3d ", i,
1752 1.1 christos host_address_to_string (arg),
1753 1.1 christos TYPE_LENGTH (arg_type));
1754 1.1 christos switch (TYPE_CODE (arg_type))
1755 1.1 christos {
1756 1.1 christos case TYPE_CODE_INT:
1757 1.1 christos fprintf_unfiltered (gdb_stdlog, "int");
1758 1.1 christos break;
1759 1.1 christos case TYPE_CODE_STRUCT:
1760 1.1 christos fprintf_unfiltered (gdb_stdlog, "struct");
1761 1.1 christos break;
1762 1.1 christos default:
1763 1.1 christos fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
1764 1.1 christos break;
1765 1.1 christos }
1766 1.1 christos fprintf_unfiltered (gdb_stdlog, " %s\n",
1767 1.1 christos host_address_to_string (value_contents (arg)));
1768 1.1 christos }
1769 1.1 christos }
1770 1.1 christos
1771 1.1 christos /* First loop: collect information.
1772 1.1 christos Cast into type_long. (This shouldn't happen often for C because
1773 1.1 christos GDB already does this earlier.) It's possible that GDB could
1774 1.1 christos do it all the time but it's harmless to leave this code here. */
1775 1.1 christos
1776 1.1 christos size = 0;
1777 1.1 christos onstack_size = 0;
1778 1.1 christos i = 0;
1779 1.1 christos
1780 1.1 christos if (struct_return)
1781 1.1 christos size = REGISTER_SIZE;
1782 1.1 christos
1783 1.1 christos for (i = 0; i < nargs; i++)
1784 1.1 christos {
1785 1.1 christos struct argument_info *info = &arg_info[i];
1786 1.1 christos struct value *arg = args[i];
1787 1.1 christos struct type *arg_type = check_typedef (value_type (arg));
1788 1.1 christos
1789 1.1 christos switch (TYPE_CODE (arg_type))
1790 1.1 christos {
1791 1.1 christos case TYPE_CODE_INT:
1792 1.1 christos case TYPE_CODE_BOOL:
1793 1.1 christos case TYPE_CODE_CHAR:
1794 1.1 christos case TYPE_CODE_RANGE:
1795 1.1 christos case TYPE_CODE_ENUM:
1796 1.1 christos
1797 1.1 christos /* Cast argument to long if necessary as the mask does it too. */
1798 1.1 christos if (TYPE_LENGTH (arg_type)
1799 1.1 christos < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
1800 1.1 christos {
1801 1.1 christos arg_type = builtin_type (gdbarch)->builtin_long;
1802 1.1 christos arg = value_cast (arg_type, arg);
1803 1.1 christos }
1804 1.1 christos /* Aligment is equal to the type length for the basic types. */
1805 1.1 christos info->align = TYPE_LENGTH (arg_type);
1806 1.1 christos break;
1807 1.1 christos
1808 1.1 christos case TYPE_CODE_FLT:
1809 1.1 christos
1810 1.1 christos /* Align doubles correctly. */
1811 1.1 christos if (TYPE_LENGTH (arg_type)
1812 1.1 christos == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
1813 1.1 christos info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
1814 1.1 christos else
1815 1.1 christos info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1816 1.1 christos break;
1817 1.1 christos
1818 1.1 christos case TYPE_CODE_STRUCT:
1819 1.1 christos default:
1820 1.1 christos info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
1821 1.1 christos break;
1822 1.1 christos }
1823 1.1 christos info->length = TYPE_LENGTH (arg_type);
1824 1.1 christos info->contents = value_contents (arg);
1825 1.1 christos
1826 1.1 christos /* Align size and onstack_size. */
1827 1.1 christos size = (size + info->align - 1) & ~(info->align - 1);
1828 1.1 christos onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
1829 1.1 christos
1830 1.1 christos if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
1831 1.1 christos {
1832 1.1 christos info->onstack = 1;
1833 1.1 christos info->u.offset = onstack_size;
1834 1.1 christos onstack_size += info->length;
1835 1.1 christos }
1836 1.1 christos else
1837 1.1 christos {
1838 1.1 christos info->onstack = 0;
1839 1.1 christos info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
1840 1.1 christos }
1841 1.1 christos size += info->length;
1842 1.1 christos }
1843 1.1 christos
1844 1.1 christos /* Adjust the stack pointer and align it. */
1845 1.1 christos sp = align_down (sp - onstack_size, SP_ALIGNMENT);
1846 1.1 christos
1847 1.1 christos /* Simulate MOVSP, if Windowed ABI. */
1848 1.1 christos if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1849 1.1 christos && (sp != osp))
1850 1.1 christos {
1851 1.1 christos read_memory (osp - 16, buf, 16);
1852 1.1 christos write_memory (sp - 16, buf, 16);
1853 1.1 christos }
1854 1.1 christos
1855 1.1 christos /* Second Loop: Load arguments. */
1856 1.1 christos
1857 1.1 christos if (struct_return)
1858 1.1 christos {
1859 1.1 christos store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
1860 1.1 christos regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
1861 1.1 christos }
1862 1.1 christos
1863 1.1 christos for (i = 0; i < nargs; i++)
1864 1.1 christos {
1865 1.1 christos struct argument_info *info = &arg_info[i];
1866 1.1 christos
1867 1.1 christos if (info->onstack)
1868 1.1 christos {
1869 1.1 christos int n = info->length;
1870 1.1 christos CORE_ADDR offset = sp + info->u.offset;
1871 1.1 christos
1872 1.1 christos /* Odd-sized structs are aligned to the lower side of a memory
1873 1.1 christos word in big-endian mode and require a shift. This only
1874 1.1 christos applies for structures smaller than one word. */
1875 1.1 christos
1876 1.1 christos if (n < REGISTER_SIZE
1877 1.1 christos && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1878 1.1 christos offset += (REGISTER_SIZE - n);
1879 1.1 christos
1880 1.1 christos write_memory (offset, info->contents, info->length);
1881 1.1 christos
1882 1.1 christos }
1883 1.1 christos else
1884 1.1 christos {
1885 1.1 christos int n = info->length;
1886 1.1 christos const bfd_byte *cp = info->contents;
1887 1.1 christos int r = info->u.regno;
1888 1.1 christos
1889 1.1 christos /* Odd-sized structs are aligned to the lower side of registers in
1890 1.1 christos big-endian mode and require a shift. The odd-sized leftover will
1891 1.1 christos be at the end. Note that this is only true for structures smaller
1892 1.1 christos than REGISTER_SIZE; for larger odd-sized structures the excess
1893 1.1 christos will be left-aligned in the register on both endiannesses. */
1894 1.1 christos
1895 1.1 christos if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
1896 1.1 christos {
1897 1.1 christos ULONGEST v;
1898 1.1 christos v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
1899 1.1 christos v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
1900 1.1 christos
1901 1.1 christos store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
1902 1.1 christos regcache_cooked_write (regcache, r, buf);
1903 1.1 christos
1904 1.1 christos cp += REGISTER_SIZE;
1905 1.1 christos n -= REGISTER_SIZE;
1906 1.1 christos r++;
1907 1.1 christos }
1908 1.1 christos else
1909 1.1 christos while (n > 0)
1910 1.1 christos {
1911 1.1 christos regcache_cooked_write (regcache, r, cp);
1912 1.1 christos
1913 1.1 christos cp += REGISTER_SIZE;
1914 1.1 christos n -= REGISTER_SIZE;
1915 1.1 christos r++;
1916 1.1 christos }
1917 1.1 christos }
1918 1.1 christos }
1919 1.1 christos
1920 1.1 christos /* Set the return address of dummy frame to the dummy address.
1921 1.1 christos The return address for the current function (in A0) is
1922 1.1 christos saved in the dummy frame, so we can savely overwrite A0 here. */
1923 1.1 christos
1924 1.1 christos if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
1925 1.1 christos {
1926 1.1 christos ULONGEST val;
1927 1.1 christos
1928 1.1 christos ra = (bp_addr & 0x3fffffff) | 0x40000000;
1929 1.1 christos regcache_raw_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), &val);
1930 1.1 christos ps = (unsigned long) val & ~0x00030000;
1931 1.1 christos regcache_cooked_write_unsigned
1932 1.1 christos (regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
1933 1.1 christos regcache_cooked_write_unsigned (regcache,
1934 1.1 christos gdbarch_ps_regnum (gdbarch),
1935 1.1 christos ps | 0x00010000);
1936 1.1 christos
1937 1.1 christos /* All the registers have been saved. After executing
1938 1.1 christos dummy call, they all will be restored. So it's safe
1939 1.1 christos to modify WINDOWSTART register to make it look like there
1940 1.1 christos is only one register window corresponding to WINDOWEBASE. */
1941 1.1 christos
1942 1.1 christos regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
1943 1.1 christos regcache_cooked_write_unsigned
1944 1.1 christos (regcache, gdbarch_tdep (gdbarch)->ws_regnum,
1945 1.1 christos 1 << extract_unsigned_integer (buf, 4, byte_order));
1946 1.1 christos }
1947 1.1 christos else
1948 1.1 christos {
1949 1.1 christos /* Simulate CALL0: write RA into A0 register. */
1950 1.1 christos regcache_cooked_write_unsigned
1951 1.1 christos (regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
1952 1.1 christos }
1953 1.1 christos
1954 1.1 christos /* Set new stack pointer and return it. */
1955 1.1 christos regcache_cooked_write_unsigned (regcache,
1956 1.1 christos gdbarch_tdep (gdbarch)->a0_base + 1, sp);
1957 1.1 christos /* Make dummy frame ID unique by adding a constant. */
1958 1.1 christos return sp + SP_ALIGNMENT;
1959 1.1 christos }
1960 1.1 christos
1961 1.1 christos
1962 1.1 christos /* Return a breakpoint for the current location of PC. We always use
1963 1.1 christos the density version if we have density instructions (regardless of the
1964 1.1 christos current instruction at PC), and use regular instructions otherwise. */
1965 1.1 christos
1966 1.1 christos #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
1967 1.1 christos #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
1968 1.1 christos #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
1969 1.1 christos #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
1970 1.1 christos
1971 1.1 christos static const unsigned char *
1972 1.1 christos xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1973 1.1 christos int *lenptr)
1974 1.1 christos {
1975 1.1 christos static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
1976 1.1 christos static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
1977 1.1 christos static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
1978 1.1 christos static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;
1979 1.1 christos
1980 1.1 christos DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
1981 1.1 christos
1982 1.1 christos if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
1983 1.1 christos {
1984 1.1 christos if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1985 1.1 christos {
1986 1.1 christos *lenptr = sizeof (density_big_breakpoint);
1987 1.1 christos return density_big_breakpoint;
1988 1.1 christos }
1989 1.1 christos else
1990 1.1 christos {
1991 1.1 christos *lenptr = sizeof (density_little_breakpoint);
1992 1.1 christos return density_little_breakpoint;
1993 1.1 christos }
1994 1.1 christos }
1995 1.1 christos else
1996 1.1 christos {
1997 1.1 christos if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1998 1.1 christos {
1999 1.1 christos *lenptr = sizeof (big_breakpoint);
2000 1.1 christos return big_breakpoint;
2001 1.1 christos }
2002 1.1 christos else
2003 1.1 christos {
2004 1.1 christos *lenptr = sizeof (little_breakpoint);
2005 1.1 christos return little_breakpoint;
2006 1.1 christos }
2007 1.1 christos }
2008 1.1 christos }
2009 1.1 christos
2010 1.1 christos /* Call0 ABI support routines. */
2011 1.1 christos
2012 1.1 christos /* Return true, if PC points to "ret" or "ret.n". */
2013 1.1 christos
2014 1.1 christos static int
2015 1.1 christos call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
2016 1.1 christos {
2017 1.1 christos #define RETURN_RET goto done
2018 1.1 christos xtensa_isa isa;
2019 1.1 christos xtensa_insnbuf ins, slot;
2020 1.1 christos gdb_byte ibuf[XTENSA_ISA_BSZ];
2021 1.1 christos CORE_ADDR ia, bt, ba;
2022 1.1 christos xtensa_format ifmt;
2023 1.1 christos int ilen, islots, is;
2024 1.1 christos xtensa_opcode opc;
2025 1.1 christos const char *opcname;
2026 1.1 christos int found_ret = 0;
2027 1.1 christos
2028 1.1 christos isa = xtensa_default_isa;
2029 1.1 christos gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2030 1.1 christos ins = xtensa_insnbuf_alloc (isa);
2031 1.1 christos slot = xtensa_insnbuf_alloc (isa);
2032 1.1 christos ba = 0;
2033 1.1 christos
2034 1.1 christos for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
2035 1.1 christos {
2036 1.1 christos if (ia + xtensa_isa_maxlength (isa) > bt)
2037 1.1 christos {
2038 1.1 christos ba = ia;
2039 1.1 christos bt = (ba + XTENSA_ISA_BSZ) < finish_pc
2040 1.1 christos ? ba + XTENSA_ISA_BSZ : finish_pc;
2041 1.1 christos if (target_read_memory (ba, ibuf, bt - ba) != 0 )
2042 1.1 christos RETURN_RET;
2043 1.1 christos }
2044 1.1 christos
2045 1.1 christos xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2046 1.1 christos ifmt = xtensa_format_decode (isa, ins);
2047 1.1 christos if (ifmt == XTENSA_UNDEFINED)
2048 1.1 christos RETURN_RET;
2049 1.1 christos ilen = xtensa_format_length (isa, ifmt);
2050 1.1 christos if (ilen == XTENSA_UNDEFINED)
2051 1.1 christos RETURN_RET;
2052 1.1 christos islots = xtensa_format_num_slots (isa, ifmt);
2053 1.1 christos if (islots == XTENSA_UNDEFINED)
2054 1.1 christos RETURN_RET;
2055 1.1 christos
2056 1.1 christos for (is = 0; is < islots; ++is)
2057 1.1 christos {
2058 1.1 christos if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
2059 1.1 christos RETURN_RET;
2060 1.1 christos
2061 1.1 christos opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2062 1.1 christos if (opc == XTENSA_UNDEFINED)
2063 1.1 christos RETURN_RET;
2064 1.1 christos
2065 1.1 christos opcname = xtensa_opcode_name (isa, opc);
2066 1.1 christos
2067 1.1 christos if ((strcasecmp (opcname, "ret.n") == 0)
2068 1.1 christos || (strcasecmp (opcname, "ret") == 0))
2069 1.1 christos {
2070 1.1 christos found_ret = 1;
2071 1.1 christos RETURN_RET;
2072 1.1 christos }
2073 1.1 christos }
2074 1.1 christos }
2075 1.1 christos done:
2076 1.1 christos xtensa_insnbuf_free(isa, slot);
2077 1.1 christos xtensa_insnbuf_free(isa, ins);
2078 1.1 christos return found_ret;
2079 1.1 christos }
2080 1.1 christos
2081 1.1 christos /* Call0 opcode class. Opcodes are preclassified according to what they
2082 1.1 christos mean for Call0 prologue analysis, and their number of significant operands.
2083 1.1 christos The purpose of this is to simplify prologue analysis by separating
2084 1.1 christos instruction decoding (libisa) from the semantics of prologue analysis. */
2085 1.1 christos
2086 1.1 christos typedef enum
2087 1.1 christos {
2088 1.1 christos c0opc_illegal, /* Unknown to libisa (invalid) or 'ill' opcode. */
2089 1.1 christos c0opc_uninteresting, /* Not interesting for Call0 prologue analysis. */
2090 1.1 christos c0opc_flow, /* Flow control insn. */
2091 1.1 christos c0opc_entry, /* ENTRY indicates non-Call0 prologue. */
2092 1.1 christos c0opc_break, /* Debugger software breakpoints. */
2093 1.1 christos c0opc_add, /* Adding two registers. */
2094 1.1 christos c0opc_addi, /* Adding a register and an immediate. */
2095 1.1 christos c0opc_and, /* Bitwise "and"-ing two registers. */
2096 1.1 christos c0opc_sub, /* Subtracting a register from a register. */
2097 1.1 christos c0opc_mov, /* Moving a register to a register. */
2098 1.1 christos c0opc_movi, /* Moving an immediate to a register. */
2099 1.1 christos c0opc_l32r, /* Loading a literal. */
2100 1.1 christos c0opc_s32i, /* Storing word at fixed offset from a base register. */
2101 1.1 christos c0opc_rwxsr, /* RSR, WRS, or XSR instructions. */
2102 1.1 christos c0opc_l32e, /* L32E instruction. */
2103 1.1 christos c0opc_s32e, /* S32E instruction. */
2104 1.1 christos c0opc_rfwo, /* RFWO instruction. */
2105 1.1 christos c0opc_rfwu, /* RFWU instruction. */
2106 1.1 christos c0opc_NrOf /* Number of opcode classifications. */
2107 1.1 christos } xtensa_insn_kind;
2108 1.1 christos
2109 1.1 christos /* Return true, if OPCNAME is RSR, WRS, or XSR instruction. */
2110 1.1 christos
2111 1.1 christos static int
2112 1.1 christos rwx_special_register (const char *opcname)
2113 1.1 christos {
2114 1.1 christos char ch = *opcname++;
2115 1.1 christos
2116 1.1 christos if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
2117 1.1 christos return 0;
2118 1.1 christos if (*opcname++ != 's')
2119 1.1 christos return 0;
2120 1.1 christos if (*opcname++ != 'r')
2121 1.1 christos return 0;
2122 1.1 christos if (*opcname++ != '.')
2123 1.1 christos return 0;
2124 1.1 christos
2125 1.1 christos return 1;
2126 1.1 christos }
2127 1.1 christos
2128 1.1 christos /* Classify an opcode based on what it means for Call0 prologue analysis. */
2129 1.1 christos
2130 1.1 christos static xtensa_insn_kind
2131 1.1 christos call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
2132 1.1 christos {
2133 1.1 christos const char *opcname;
2134 1.1 christos xtensa_insn_kind opclass = c0opc_uninteresting;
2135 1.1 christos
2136 1.1 christos DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
2137 1.1 christos
2138 1.1 christos /* Get opcode name and handle special classifications. */
2139 1.1 christos
2140 1.1 christos opcname = xtensa_opcode_name (isa, opc);
2141 1.1 christos
2142 1.1 christos if (opcname == NULL
2143 1.1 christos || strcasecmp (opcname, "ill") == 0
2144 1.1 christos || strcasecmp (opcname, "ill.n") == 0)
2145 1.1 christos opclass = c0opc_illegal;
2146 1.1 christos else if (strcasecmp (opcname, "break") == 0
2147 1.1 christos || strcasecmp (opcname, "break.n") == 0)
2148 1.1 christos opclass = c0opc_break;
2149 1.1 christos else if (strcasecmp (opcname, "entry") == 0)
2150 1.1 christos opclass = c0opc_entry;
2151 1.1 christos else if (strcasecmp (opcname, "rfwo") == 0)
2152 1.1 christos opclass = c0opc_rfwo;
2153 1.1 christos else if (strcasecmp (opcname, "rfwu") == 0)
2154 1.1 christos opclass = c0opc_rfwu;
2155 1.1 christos else if (xtensa_opcode_is_branch (isa, opc) > 0
2156 1.1 christos || xtensa_opcode_is_jump (isa, opc) > 0
2157 1.1 christos || xtensa_opcode_is_loop (isa, opc) > 0
2158 1.1 christos || xtensa_opcode_is_call (isa, opc) > 0
2159 1.1 christos || strcasecmp (opcname, "simcall") == 0
2160 1.1 christos || strcasecmp (opcname, "syscall") == 0)
2161 1.1 christos opclass = c0opc_flow;
2162 1.1 christos
2163 1.1 christos /* Also, classify specific opcodes that need to be tracked. */
2164 1.1 christos else if (strcasecmp (opcname, "add") == 0
2165 1.1 christos || strcasecmp (opcname, "add.n") == 0)
2166 1.1 christos opclass = c0opc_add;
2167 1.1 christos else if (strcasecmp (opcname, "and") == 0)
2168 1.1 christos opclass = c0opc_and;
2169 1.1 christos else if (strcasecmp (opcname, "addi") == 0
2170 1.1 christos || strcasecmp (opcname, "addi.n") == 0
2171 1.1 christos || strcasecmp (opcname, "addmi") == 0)
2172 1.1 christos opclass = c0opc_addi;
2173 1.1 christos else if (strcasecmp (opcname, "sub") == 0)
2174 1.1 christos opclass = c0opc_sub;
2175 1.1 christos else if (strcasecmp (opcname, "mov.n") == 0
2176 1.1 christos || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro. */
2177 1.1 christos opclass = c0opc_mov;
2178 1.1 christos else if (strcasecmp (opcname, "movi") == 0
2179 1.1 christos || strcasecmp (opcname, "movi.n") == 0)
2180 1.1 christos opclass = c0opc_movi;
2181 1.1 christos else if (strcasecmp (opcname, "l32r") == 0)
2182 1.1 christos opclass = c0opc_l32r;
2183 1.1 christos else if (strcasecmp (opcname, "s32i") == 0
2184 1.1 christos || strcasecmp (opcname, "s32i.n") == 0)
2185 1.1 christos opclass = c0opc_s32i;
2186 1.1 christos else if (strcasecmp (opcname, "l32e") == 0)
2187 1.1 christos opclass = c0opc_l32e;
2188 1.1 christos else if (strcasecmp (opcname, "s32e") == 0)
2189 1.1 christos opclass = c0opc_s32e;
2190 1.1 christos else if (rwx_special_register (opcname))
2191 1.1 christos opclass = c0opc_rwxsr;
2192 1.1 christos
2193 1.1 christos return opclass;
2194 1.1 christos }
2195 1.1 christos
2196 1.1 christos /* Tracks register movement/mutation for a given operation, which may
2197 1.1 christos be within a bundle. Updates the destination register tracking info
2198 1.1 christos accordingly. The pc is needed only for pc-relative load instructions
2199 1.1 christos (eg. l32r). The SP register number is needed to identify stores to
2200 1.1 christos the stack frame. Returns 0, if analysis was succesfull, non-zero
2201 1.1 christos otherwise. */
2202 1.1 christos
2203 1.1 christos static int
2204 1.1 christos call0_track_op (struct gdbarch *gdbarch, xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
2205 1.1 christos xtensa_insn_kind opclass, int nods, unsigned odv[],
2206 1.1 christos CORE_ADDR pc, int spreg, xtensa_frame_cache_t *cache)
2207 1.1 christos {
2208 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2209 1.1 christos unsigned litbase, litaddr, litval;
2210 1.1 christos
2211 1.1 christos switch (opclass)
2212 1.1 christos {
2213 1.1 christos case c0opc_addi:
2214 1.1 christos /* 3 operands: dst, src, imm. */
2215 1.1 christos gdb_assert (nods == 3);
2216 1.1 christos dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2217 1.1 christos dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
2218 1.1 christos break;
2219 1.1 christos case c0opc_add:
2220 1.1 christos /* 3 operands: dst, src1, src2. */
2221 1.1 christos gdb_assert (nods == 3);
2222 1.1 christos if (src[odv[1]].fr_reg == C0_CONST)
2223 1.1 christos {
2224 1.1 christos dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
2225 1.1 christos dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
2226 1.1 christos }
2227 1.1 christos else if (src[odv[2]].fr_reg == C0_CONST)
2228 1.1 christos {
2229 1.1 christos dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2230 1.1 christos dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
2231 1.1 christos }
2232 1.1 christos else dst[odv[0]].fr_reg = C0_INEXP;
2233 1.1 christos break;
2234 1.1 christos case c0opc_and:
2235 1.1 christos /* 3 operands: dst, src1, src2. */
2236 1.1 christos gdb_assert (nods == 3);
2237 1.1 christos if (cache->c0.c0_fpalign == 0)
2238 1.1 christos {
2239 1.1 christos /* Handle dynamic stack alignment. */
2240 1.1 christos if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
2241 1.1 christos {
2242 1.1 christos if (src[odv[2]].fr_reg == C0_CONST)
2243 1.1 christos cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
2244 1.1 christos break;
2245 1.1 christos }
2246 1.1 christos else if ((src[odv[0]].fr_reg == spreg)
2247 1.1 christos && (src[odv[2]].fr_reg == spreg))
2248 1.1 christos {
2249 1.1 christos if (src[odv[1]].fr_reg == C0_CONST)
2250 1.1 christos cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
2251 1.1 christos break;
2252 1.1 christos }
2253 1.1 christos /* else fall through. */
2254 1.1 christos }
2255 1.1 christos if (src[odv[1]].fr_reg == C0_CONST)
2256 1.1 christos {
2257 1.1 christos dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
2258 1.1 christos dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs & src[odv[1]].fr_ofs;
2259 1.1 christos }
2260 1.1 christos else if (src[odv[2]].fr_reg == C0_CONST)
2261 1.1 christos {
2262 1.1 christos dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2263 1.1 christos dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs & src[odv[2]].fr_ofs;
2264 1.1 christos }
2265 1.1 christos else dst[odv[0]].fr_reg = C0_INEXP;
2266 1.1 christos break;
2267 1.1 christos case c0opc_sub:
2268 1.1 christos /* 3 operands: dst, src1, src2. */
2269 1.1 christos gdb_assert (nods == 3);
2270 1.1 christos if (src[odv[2]].fr_reg == C0_CONST)
2271 1.1 christos {
2272 1.1 christos dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2273 1.1 christos dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
2274 1.1 christos }
2275 1.1 christos else dst[odv[0]].fr_reg = C0_INEXP;
2276 1.1 christos break;
2277 1.1 christos case c0opc_mov:
2278 1.1 christos /* 2 operands: dst, src [, src]. */
2279 1.1 christos gdb_assert (nods == 2);
2280 1.1 christos /* First, check if it's a special case of saving unaligned SP
2281 1.1 christos to a spare register in case of dynamic stack adjustment.
2282 1.1 christos But, only do it one time. The second time could be initializing
2283 1.1 christos frame pointer. We don't want to overwrite the first one. */
2284 1.1 christos if ((odv[1] == spreg) && (cache->c0.c0_old_sp == C0_INEXP))
2285 1.1 christos cache->c0.c0_old_sp = odv[0];
2286 1.1 christos
2287 1.1 christos dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
2288 1.1 christos dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
2289 1.1 christos break;
2290 1.1 christos case c0opc_movi:
2291 1.1 christos /* 2 operands: dst, imm. */
2292 1.1 christos gdb_assert (nods == 2);
2293 1.1 christos dst[odv[0]].fr_reg = C0_CONST;
2294 1.1 christos dst[odv[0]].fr_ofs = odv[1];
2295 1.1 christos break;
2296 1.1 christos case c0opc_l32r:
2297 1.1 christos /* 2 operands: dst, literal offset. */
2298 1.1 christos gdb_assert (nods == 2);
2299 1.1 christos /* litbase = xtensa_get_litbase (pc); can be also used. */
2300 1.1 christos litbase = (gdbarch_tdep (gdbarch)->litbase_regnum == -1)
2301 1.1 christos ? 0 : xtensa_read_register
2302 1.1 christos (gdbarch_tdep (gdbarch)->litbase_regnum);
2303 1.1 christos litaddr = litbase & 1
2304 1.1 christos ? (litbase & ~1) + (signed)odv[1]
2305 1.1 christos : (pc + 3 + (signed)odv[1]) & ~3;
2306 1.1 christos litval = read_memory_integer (litaddr, 4, byte_order);
2307 1.1 christos dst[odv[0]].fr_reg = C0_CONST;
2308 1.1 christos dst[odv[0]].fr_ofs = litval;
2309 1.1 christos break;
2310 1.1 christos case c0opc_s32i:
2311 1.1 christos /* 3 operands: value, base, offset. */
2312 1.1 christos gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
2313 1.1 christos /* First, check if it's a spill for saved unaligned SP,
2314 1.1 christos when dynamic stack adjustment was applied to this frame. */
2315 1.1 christos if ((cache->c0.c0_fpalign != 0) /* Dynamic stack adjustment. */
2316 1.1 christos && (odv[1] == spreg) /* SP usage indicates spill. */
2317 1.1 christos && (odv[0] == cache->c0.c0_old_sp)) /* Old SP register spilled. */
2318 1.1 christos cache->c0.c0_sp_ofs = odv[2];
2319 1.1 christos
2320 1.1 christos if (src[odv[1]].fr_reg == spreg /* Store to stack frame. */
2321 1.1 christos && (src[odv[1]].fr_ofs & 3) == 0 /* Alignment preserved. */
2322 1.1 christos && src[odv[0]].fr_reg >= 0 /* Value is from a register. */
2323 1.1 christos && src[odv[0]].fr_ofs == 0 /* Value hasn't been modified. */
2324 1.1 christos && src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time. */
2325 1.1 christos {
2326 1.1 christos /* ISA encoding guarantees alignment. But, check it anyway. */
2327 1.1 christos gdb_assert ((odv[2] & 3) == 0);
2328 1.1 christos dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
2329 1.1 christos }
2330 1.1 christos break;
2331 1.1 christos /* If we end up inside Window Overflow / Underflow interrupt handler
2332 1.1 christos report an error because these handlers should have been handled
2333 1.1 christos already in a different way. */
2334 1.1 christos case c0opc_l32e:
2335 1.1 christos case c0opc_s32e:
2336 1.1 christos case c0opc_rfwo:
2337 1.1 christos case c0opc_rfwu:
2338 1.1 christos return 1;
2339 1.1 christos default:
2340 1.1 christos return 1;
2341 1.1 christos }
2342 1.1 christos return 0;
2343 1.1 christos }
2344 1.1 christos
2345 1.1 christos /* Analyze prologue of the function at start address to determine if it uses
2346 1.1 christos the Call0 ABI, and if so track register moves and linear modifications
2347 1.1 christos in the prologue up to the PC or just beyond the prologue, whichever is
2348 1.1 christos first. An 'entry' instruction indicates non-Call0 ABI and the end of the
2349 1.1 christos prologue. The prologue may overlap non-prologue instructions but is
2350 1.1 christos guaranteed to end by the first flow-control instruction (jump, branch,
2351 1.1 christos call or return). Since an optimized function may move information around
2352 1.1 christos and change the stack frame arbitrarily during the prologue, the information
2353 1.1 christos is guaranteed valid only at the point in the function indicated by the PC.
2354 1.1 christos May be used to skip the prologue or identify the ABI, w/o tracking.
2355 1.1 christos
2356 1.1 christos Returns: Address of first instruction after prologue, or PC (whichever
2357 1.1 christos is first), or 0, if decoding failed (in libisa).
2358 1.1 christos Input args:
2359 1.1 christos start Start address of function/prologue.
2360 1.1 christos pc Program counter to stop at. Use 0 to continue to end of prologue.
2361 1.1 christos If 0, avoids infinite run-on in corrupt code memory by bounding
2362 1.1 christos the scan to the end of the function if that can be determined.
2363 1.1 christos nregs Number of general registers to track.
2364 1.1 christos InOut args:
2365 1.1 christos cache Xtensa frame cache.
2366 1.1 christos
2367 1.1 christos Note that these may produce useful results even if decoding fails
2368 1.1 christos because they begin with default assumptions that analysis may change. */
2369 1.1 christos
2370 1.1 christos static CORE_ADDR
2371 1.1 christos call0_analyze_prologue (struct gdbarch *gdbarch,
2372 1.1 christos CORE_ADDR start, CORE_ADDR pc,
2373 1.1 christos int nregs, xtensa_frame_cache_t *cache)
2374 1.1 christos {
2375 1.1 christos CORE_ADDR ia; /* Current insn address in prologue. */
2376 1.1 christos CORE_ADDR ba = 0; /* Current address at base of insn buffer. */
2377 1.1 christos CORE_ADDR bt; /* Current address at top+1 of insn buffer. */
2378 1.1 christos gdb_byte ibuf[XTENSA_ISA_BSZ];/* Instruction buffer for decoding prologue. */
2379 1.1 christos xtensa_isa isa; /* libisa ISA handle. */
2380 1.1 christos xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot. */
2381 1.1 christos xtensa_format ifmt; /* libisa instruction format. */
2382 1.1 christos int ilen, islots, is; /* Instruction length, nbr slots, current slot. */
2383 1.1 christos xtensa_opcode opc; /* Opcode in current slot. */
2384 1.1 christos xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis. */
2385 1.1 christos int nods; /* Opcode number of operands. */
2386 1.1 christos unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa. */
2387 1.1 christos xtensa_c0reg_t *rtmp; /* Register tracking info snapshot. */
2388 1.1 christos int j; /* General loop counter. */
2389 1.1 christos int fail = 0; /* Set non-zero and exit, if decoding fails. */
2390 1.1 christos CORE_ADDR body_pc; /* The PC for the first non-prologue insn. */
2391 1.1 christos CORE_ADDR end_pc; /* The PC for the lust function insn. */
2392 1.1 christos
2393 1.1 christos struct symtab_and_line prologue_sal;
2394 1.1 christos
2395 1.1 christos DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
2396 1.1 christos (int)start, (int)pc);
2397 1.1 christos
2398 1.1 christos /* Try to limit the scan to the end of the function if a non-zero pc
2399 1.1 christos arg was not supplied to avoid probing beyond the end of valid memory.
2400 1.1 christos If memory is full of garbage that classifies as c0opc_uninteresting.
2401 1.1 christos If this fails (eg. if no symbols) pc ends up 0 as it was.
2402 1.1 christos Intialize the Call0 frame and register tracking info.
2403 1.1 christos Assume it's Call0 until an 'entry' instruction is encountered.
2404 1.1 christos Assume we may be in the prologue until we hit a flow control instr. */
2405 1.1 christos
2406 1.1 christos rtmp = NULL;
2407 1.1 christos body_pc = UINT_MAX;
2408 1.1 christos end_pc = 0;
2409 1.1 christos
2410 1.1 christos /* Find out, if we have an information about the prologue from DWARF. */
2411 1.1 christos prologue_sal = find_pc_line (start, 0);
2412 1.1 christos if (prologue_sal.line != 0) /* Found debug info. */
2413 1.1 christos body_pc = prologue_sal.end;
2414 1.1 christos
2415 1.1 christos /* If we are going to analyze the prologue in general without knowing about
2416 1.1 christos the current PC, make the best assumtion for the end of the prologue. */
2417 1.1 christos if (pc == 0)
2418 1.1 christos {
2419 1.1 christos find_pc_partial_function (start, 0, NULL, &end_pc);
2420 1.1 christos body_pc = min (end_pc, body_pc);
2421 1.1 christos }
2422 1.1 christos else
2423 1.1 christos body_pc = min (pc, body_pc);
2424 1.1 christos
2425 1.1 christos cache->call0 = 1;
2426 1.1 christos rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
2427 1.1 christos
2428 1.1 christos if (!xtensa_default_isa)
2429 1.1 christos xtensa_default_isa = xtensa_isa_init (0, 0);
2430 1.1 christos isa = xtensa_default_isa;
2431 1.1 christos gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2432 1.1 christos ins = xtensa_insnbuf_alloc (isa);
2433 1.1 christos slot = xtensa_insnbuf_alloc (isa);
2434 1.1 christos
2435 1.1 christos for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
2436 1.1 christos {
2437 1.1 christos /* (Re)fill instruction buffer from memory if necessary, but do not
2438 1.1 christos read memory beyond PC to be sure we stay within text section
2439 1.1 christos (this protection only works if a non-zero pc is supplied). */
2440 1.1 christos
2441 1.1 christos if (ia + xtensa_isa_maxlength (isa) > bt)
2442 1.1 christos {
2443 1.1 christos ba = ia;
2444 1.1 christos bt = (ba + XTENSA_ISA_BSZ) < body_pc ? ba + XTENSA_ISA_BSZ : body_pc;
2445 1.1 christos if (target_read_memory (ba, ibuf, bt - ba) != 0 )
2446 1.1 christos error (_("Unable to read target memory ..."));
2447 1.1 christos }
2448 1.1 christos
2449 1.1 christos /* Decode format information. */
2450 1.1 christos
2451 1.1 christos xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2452 1.1 christos ifmt = xtensa_format_decode (isa, ins);
2453 1.1 christos if (ifmt == XTENSA_UNDEFINED)
2454 1.1 christos {
2455 1.1 christos fail = 1;
2456 1.1 christos goto done;
2457 1.1 christos }
2458 1.1 christos ilen = xtensa_format_length (isa, ifmt);
2459 1.1 christos if (ilen == XTENSA_UNDEFINED)
2460 1.1 christos {
2461 1.1 christos fail = 1;
2462 1.1 christos goto done;
2463 1.1 christos }
2464 1.1 christos islots = xtensa_format_num_slots (isa, ifmt);
2465 1.1 christos if (islots == XTENSA_UNDEFINED)
2466 1.1 christos {
2467 1.1 christos fail = 1;
2468 1.1 christos goto done;
2469 1.1 christos }
2470 1.1 christos
2471 1.1 christos /* Analyze a bundle or a single instruction, using a snapshot of
2472 1.1 christos the register tracking info as input for the entire bundle so that
2473 1.1 christos register changes do not take effect within this bundle. */
2474 1.1 christos
2475 1.1 christos for (j = 0; j < nregs; ++j)
2476 1.1 christos rtmp[j] = cache->c0.c0_rt[j];
2477 1.1 christos
2478 1.1 christos for (is = 0; is < islots; ++is)
2479 1.1 christos {
2480 1.1 christos /* Decode a slot and classify the opcode. */
2481 1.1 christos
2482 1.1 christos fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
2483 1.1 christos if (fail)
2484 1.1 christos goto done;
2485 1.1 christos
2486 1.1 christos opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2487 1.1 christos DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
2488 1.1 christos (unsigned)ia, opc);
2489 1.1 christos if (opc == XTENSA_UNDEFINED)
2490 1.1 christos opclass = c0opc_illegal;
2491 1.1 christos else
2492 1.1 christos opclass = call0_classify_opcode (isa, opc);
2493 1.1 christos
2494 1.1 christos /* Decide whether to track this opcode, ignore it, or bail out. */
2495 1.1 christos
2496 1.1 christos switch (opclass)
2497 1.1 christos {
2498 1.1 christos case c0opc_illegal:
2499 1.1 christos case c0opc_break:
2500 1.1 christos fail = 1;
2501 1.1 christos goto done;
2502 1.1 christos
2503 1.1 christos case c0opc_uninteresting:
2504 1.1 christos continue;
2505 1.1 christos
2506 1.1 christos case c0opc_flow: /* Flow control instructions stop analysis. */
2507 1.1 christos case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis. */
2508 1.1 christos goto done;
2509 1.1 christos
2510 1.1 christos case c0opc_entry:
2511 1.1 christos cache->call0 = 0;
2512 1.1 christos ia += ilen; /* Skip over 'entry' insn. */
2513 1.1 christos goto done;
2514 1.1 christos
2515 1.1 christos default:
2516 1.1 christos cache->call0 = 1;
2517 1.1 christos }
2518 1.1 christos
2519 1.1 christos /* Only expected opcodes should get this far. */
2520 1.1 christos
2521 1.1 christos /* Extract and decode the operands. */
2522 1.1 christos nods = xtensa_opcode_num_operands (isa, opc);
2523 1.1 christos if (nods == XTENSA_UNDEFINED)
2524 1.1 christos {
2525 1.1 christos fail = 1;
2526 1.1 christos goto done;
2527 1.1 christos }
2528 1.1 christos
2529 1.1 christos for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
2530 1.1 christos {
2531 1.1 christos fail = xtensa_operand_get_field (isa, opc, j, ifmt,
2532 1.1 christos is, slot, &odv[j]);
2533 1.1 christos if (fail)
2534 1.1 christos goto done;
2535 1.1 christos
2536 1.1 christos fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
2537 1.1 christos if (fail)
2538 1.1 christos goto done;
2539 1.1 christos }
2540 1.1 christos
2541 1.1 christos /* Check operands to verify use of 'mov' assembler macro. */
2542 1.1 christos if (opclass == c0opc_mov && nods == 3)
2543 1.1 christos {
2544 1.1 christos if (odv[2] == odv[1])
2545 1.1 christos {
2546 1.1 christos nods = 2;
2547 1.1 christos if ((odv[0] == 1) && (odv[1] != 1))
2548 1.1 christos /* OR A1, An, An , where n != 1.
2549 1.1 christos This means we are inside epilogue already. */
2550 1.1 christos goto done;
2551 1.1 christos }
2552 1.1 christos else
2553 1.1 christos {
2554 1.1 christos opclass = c0opc_uninteresting;
2555 1.1 christos continue;
2556 1.1 christos }
2557 1.1 christos }
2558 1.1 christos
2559 1.1 christos /* Track register movement and modification for this operation. */
2560 1.1 christos fail = call0_track_op (gdbarch, cache->c0.c0_rt, rtmp,
2561 1.1 christos opclass, nods, odv, ia, 1, cache);
2562 1.1 christos if (fail)
2563 1.1 christos goto done;
2564 1.1 christos }
2565 1.1 christos }
2566 1.1 christos done:
2567 1.1 christos DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
2568 1.1 christos (unsigned)ia, fail ? "failed" : "succeeded");
2569 1.1 christos xtensa_insnbuf_free(isa, slot);
2570 1.1 christos xtensa_insnbuf_free(isa, ins);
2571 1.1 christos return fail ? XTENSA_ISA_BADPC : ia;
2572 1.1 christos }
2573 1.1 christos
2574 1.1 christos /* Initialize frame cache for the current frame in CALL0 ABI. */
2575 1.1 christos
2576 1.1 christos static void
2577 1.1 christos call0_frame_cache (struct frame_info *this_frame,
2578 1.1 christos xtensa_frame_cache_t *cache, CORE_ADDR pc)
2579 1.1 christos {
2580 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
2581 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2582 1.1 christos CORE_ADDR start_pc; /* The beginning of the function. */
2583 1.1 christos CORE_ADDR body_pc=UINT_MAX; /* PC, where prologue analysis stopped. */
2584 1.1 christos CORE_ADDR sp, fp, ra;
2585 1.1 christos int fp_regnum = C0_SP, c0_hasfp = 0, c0_frmsz = 0, prev_sp = 0, to_stk;
2586 1.1 christos
2587 1.1 christos sp = get_frame_register_unsigned
2588 1.1 christos (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
2589 1.1 christos fp = sp; /* Assume FP == SP until proven otherwise. */
2590 1.1 christos
2591 1.1 christos /* Find the beginning of the prologue of the function containing the PC
2592 1.1 christos and analyze it up to the PC or the end of the prologue. */
2593 1.1 christos
2594 1.1 christos if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
2595 1.1 christos {
2596 1.1 christos body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);
2597 1.1 christos
2598 1.1 christos if (body_pc == XTENSA_ISA_BADPC)
2599 1.1 christos {
2600 1.1 christos warning_once ();
2601 1.1 christos ra = 0;
2602 1.1 christos goto finish_frame_analysis;
2603 1.1 christos }
2604 1.1 christos }
2605 1.1 christos
2606 1.1 christos /* Get the frame information and FP (if used) at the current PC.
2607 1.1 christos If PC is in the prologue, the prologue analysis is more reliable
2608 1.1 christos than DWARF info. We don't not know for sure, if PC is in the prologue,
2609 1.1 christos but we do know no calls have yet taken place, so we can almost
2610 1.1 christos certainly rely on the prologue analysis. */
2611 1.1 christos
2612 1.1 christos if (body_pc <= pc)
2613 1.1 christos {
2614 1.1 christos /* Prologue analysis was successful up to the PC.
2615 1.1 christos It includes the cases when PC == START_PC. */
2616 1.1 christos c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
2617 1.1 christos /* c0_hasfp == true means there is a frame pointer because
2618 1.1 christos we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
2619 1.1 christos was derived from SP. Otherwise, it would be C0_FP. */
2620 1.1 christos fp_regnum = c0_hasfp ? C0_FP : C0_SP;
2621 1.1 christos c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
2622 1.1 christos fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
2623 1.1 christos }
2624 1.1 christos else /* No data from the prologue analysis. */
2625 1.1 christos {
2626 1.1 christos c0_hasfp = 0;
2627 1.1 christos fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
2628 1.1 christos c0_frmsz = 0;
2629 1.1 christos start_pc = pc;
2630 1.1 christos }
2631 1.1 christos
2632 1.1 christos if (cache->c0.c0_fpalign)
2633 1.1 christos {
2634 1.1 christos /* This frame has a special prologue with a dynamic stack adjustment
2635 1.1 christos to force an alignment, which is bigger than standard 16 bytes. */
2636 1.1 christos
2637 1.1 christos CORE_ADDR unaligned_sp;
2638 1.1 christos
2639 1.1 christos if (cache->c0.c0_old_sp == C0_INEXP)
2640 1.1 christos /* This can't be. Prologue code should be consistent.
2641 1.1 christos Unaligned stack pointer should be saved in a spare register. */
2642 1.1 christos {
2643 1.1 christos warning_once ();
2644 1.1 christos ra = 0;
2645 1.1 christos goto finish_frame_analysis;
2646 1.1 christos }
2647 1.1 christos
2648 1.1 christos if (cache->c0.c0_sp_ofs == C0_NOSTK)
2649 1.1 christos /* Saved unaligned value of SP is kept in a register. */
2650 1.1 christos unaligned_sp = get_frame_register_unsigned
2651 1.1 christos (this_frame, gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_old_sp);
2652 1.1 christos else
2653 1.1 christos /* Get the value from stack. */
2654 1.1 christos unaligned_sp = (CORE_ADDR)
2655 1.1 christos read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);
2656 1.1 christos
2657 1.1 christos prev_sp = unaligned_sp + c0_frmsz;
2658 1.1 christos }
2659 1.1 christos else
2660 1.1 christos prev_sp = fp + c0_frmsz;
2661 1.1 christos
2662 1.1 christos /* Frame size from debug info or prologue tracking does not account for
2663 1.1 christos alloca() and other dynamic allocations. Adjust frame size by FP - SP. */
2664 1.1 christos if (c0_hasfp)
2665 1.1 christos {
2666 1.1 christos fp = get_frame_register_unsigned (this_frame, fp_regnum);
2667 1.1 christos
2668 1.1 christos /* Update the stack frame size. */
2669 1.1 christos c0_frmsz += fp - sp;
2670 1.1 christos }
2671 1.1 christos
2672 1.1 christos /* Get the return address (RA) from the stack if saved,
2673 1.1 christos or try to get it from a register. */
2674 1.1 christos
2675 1.1 christos to_stk = cache->c0.c0_rt[C0_RA].to_stk;
2676 1.1 christos if (to_stk != C0_NOSTK)
2677 1.1 christos ra = (CORE_ADDR)
2678 1.1 christos read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
2679 1.1 christos 4, byte_order);
2680 1.1 christos
2681 1.1 christos else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
2682 1.1 christos && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
2683 1.1 christos {
2684 1.1 christos /* Special case for terminating backtrace at a function that wants to
2685 1.1 christos be seen as the outermost one. Such a function will clear it's RA (A0)
2686 1.1 christos register to 0 in the prologue instead of saving its original value. */
2687 1.1 christos ra = 0;
2688 1.1 christos }
2689 1.1 christos else
2690 1.1 christos {
2691 1.1 christos /* RA was copied to another register or (before any function call) may
2692 1.1 christos still be in the original RA register. This is not always reliable:
2693 1.1 christos even in a leaf function, register tracking stops after prologue, and
2694 1.1 christos even in prologue, non-prologue instructions (not tracked) may overwrite
2695 1.1 christos RA or any register it was copied to. If likely in prologue or before
2696 1.1 christos any call, use retracking info and hope for the best (compiler should
2697 1.1 christos have saved RA in stack if not in a leaf function). If not in prologue,
2698 1.1 christos too bad. */
2699 1.1 christos
2700 1.1 christos int i;
2701 1.1 christos for (i = 0;
2702 1.1 christos (i < C0_NREGS)
2703 1.1 christos && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
2704 1.1 christos ++i);
2705 1.1 christos if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
2706 1.1 christos i = C0_RA;
2707 1.1 christos if (i < C0_NREGS)
2708 1.1 christos {
2709 1.1 christos ra = get_frame_register_unsigned
2710 1.1 christos (this_frame,
2711 1.1 christos gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
2712 1.1 christos }
2713 1.1 christos else ra = 0;
2714 1.1 christos }
2715 1.1 christos
2716 1.1 christos finish_frame_analysis:
2717 1.1 christos cache->pc = start_pc;
2718 1.1 christos cache->ra = ra;
2719 1.1 christos /* RA == 0 marks the outermost frame. Do not go past it. */
2720 1.1 christos cache->prev_sp = (ra != 0) ? prev_sp : 0;
2721 1.1 christos cache->c0.fp_regnum = fp_regnum;
2722 1.1 christos cache->c0.c0_frmsz = c0_frmsz;
2723 1.1 christos cache->c0.c0_hasfp = c0_hasfp;
2724 1.1 christos cache->c0.c0_fp = fp;
2725 1.1 christos }
2726 1.1 christos
2727 1.1 christos static CORE_ADDR a0_saved;
2728 1.1 christos static CORE_ADDR a7_saved;
2729 1.1 christos static CORE_ADDR a11_saved;
2730 1.1 christos static int a0_was_saved;
2731 1.1 christos static int a7_was_saved;
2732 1.1 christos static int a11_was_saved;
2733 1.1 christos
2734 1.1 christos /* Simulate L32E instruction: AT <-- ref (AS + offset). */
2735 1.1 christos static void
2736 1.1 christos execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2737 1.1 christos {
2738 1.1 christos int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
2739 1.1 christos int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
2740 1.1 christos CORE_ADDR addr = xtensa_read_register (asreg) + offset;
2741 1.1 christos unsigned int spilled_value
2742 1.1 christos = read_memory_unsigned_integer (addr, 4, gdbarch_byte_order (gdbarch));
2743 1.1 christos
2744 1.1 christos if ((at == 0) && !a0_was_saved)
2745 1.1 christos {
2746 1.1 christos a0_saved = xtensa_read_register (atreg);
2747 1.1 christos a0_was_saved = 1;
2748 1.1 christos }
2749 1.1 christos else if ((at == 7) && !a7_was_saved)
2750 1.1 christos {
2751 1.1 christos a7_saved = xtensa_read_register (atreg);
2752 1.1 christos a7_was_saved = 1;
2753 1.1 christos }
2754 1.1 christos else if ((at == 11) && !a11_was_saved)
2755 1.1 christos {
2756 1.1 christos a11_saved = xtensa_read_register (atreg);
2757 1.1 christos a11_was_saved = 1;
2758 1.1 christos }
2759 1.1 christos
2760 1.1 christos xtensa_write_register (atreg, spilled_value);
2761 1.1 christos }
2762 1.1 christos
2763 1.1 christos /* Simulate S32E instruction: AT --> ref (AS + offset). */
2764 1.1 christos static void
2765 1.1 christos execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
2766 1.1 christos {
2767 1.1 christos int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
2768 1.1 christos int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
2769 1.1 christos CORE_ADDR addr = xtensa_read_register (asreg) + offset;
2770 1.1 christos ULONGEST spilled_value = xtensa_read_register (atreg);
2771 1.1 christos
2772 1.1 christos write_memory_unsigned_integer (addr, 4,
2773 1.1 christos gdbarch_byte_order (gdbarch),
2774 1.1 christos spilled_value);
2775 1.1 christos }
2776 1.1 christos
2777 1.1 christos #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN 200
2778 1.1 christos
2779 1.1 christos typedef enum
2780 1.1 christos {
2781 1.1 christos xtWindowOverflow,
2782 1.1 christos xtWindowUnderflow,
2783 1.1 christos xtNoExceptionHandler
2784 1.1 christos } xtensa_exception_handler_t;
2785 1.1 christos
2786 1.1 christos /* Execute instruction stream from current PC until hitting RFWU or RFWO.
2787 1.1 christos Return type of Xtensa Window Interrupt Handler on success. */
2788 1.1 christos static xtensa_exception_handler_t
2789 1.1 christos execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
2790 1.1 christos {
2791 1.1 christos xtensa_isa isa;
2792 1.1 christos xtensa_insnbuf ins, slot;
2793 1.1 christos gdb_byte ibuf[XTENSA_ISA_BSZ];
2794 1.1 christos CORE_ADDR ia, bt, ba;
2795 1.1 christos xtensa_format ifmt;
2796 1.1 christos int ilen, islots, is;
2797 1.1 christos xtensa_opcode opc;
2798 1.1 christos int insn_num = 0;
2799 1.1 christos void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);
2800 1.1 christos
2801 1.1 christos uint32_t at, as, offset;
2802 1.1 christos
2803 1.1 christos /* WindowUnderflow12 = true, when inside _WindowUnderflow12. */
2804 1.1 christos int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140;
2805 1.1 christos
2806 1.1 christos isa = xtensa_default_isa;
2807 1.1 christos gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
2808 1.1 christos ins = xtensa_insnbuf_alloc (isa);
2809 1.1 christos slot = xtensa_insnbuf_alloc (isa);
2810 1.1 christos ba = 0;
2811 1.1 christos ia = current_pc;
2812 1.1 christos bt = ia;
2813 1.1 christos
2814 1.1 christos a0_was_saved = 0;
2815 1.1 christos a7_was_saved = 0;
2816 1.1 christos a11_was_saved = 0;
2817 1.1 christos
2818 1.1 christos while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
2819 1.1 christos {
2820 1.1 christos if (ia + xtensa_isa_maxlength (isa) > bt)
2821 1.1 christos {
2822 1.1 christos ba = ia;
2823 1.1 christos bt = (ba + XTENSA_ISA_BSZ);
2824 1.1 christos if (target_read_memory (ba, ibuf, bt - ba) != 0)
2825 1.1 christos return xtNoExceptionHandler;
2826 1.1 christos }
2827 1.1 christos xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2828 1.1 christos ifmt = xtensa_format_decode (isa, ins);
2829 1.1 christos if (ifmt == XTENSA_UNDEFINED)
2830 1.1 christos return xtNoExceptionHandler;
2831 1.1 christos ilen = xtensa_format_length (isa, ifmt);
2832 1.1 christos if (ilen == XTENSA_UNDEFINED)
2833 1.1 christos return xtNoExceptionHandler;
2834 1.1 christos islots = xtensa_format_num_slots (isa, ifmt);
2835 1.1 christos if (islots == XTENSA_UNDEFINED)
2836 1.1 christos return xtNoExceptionHandler;
2837 1.1 christos for (is = 0; is < islots; ++is)
2838 1.1 christos {
2839 1.1 christos if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
2840 1.1 christos return xtNoExceptionHandler;
2841 1.1 christos opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2842 1.1 christos if (opc == XTENSA_UNDEFINED)
2843 1.1 christos return xtNoExceptionHandler;
2844 1.1 christos switch (call0_classify_opcode (isa, opc))
2845 1.1 christos {
2846 1.1 christos case c0opc_illegal:
2847 1.1 christos case c0opc_flow:
2848 1.1 christos case c0opc_entry:
2849 1.1 christos case c0opc_break:
2850 1.1 christos /* We expect none of them here. */
2851 1.1 christos return xtNoExceptionHandler;
2852 1.1 christos case c0opc_l32e:
2853 1.1 christos func = execute_l32e;
2854 1.1 christos break;
2855 1.1 christos case c0opc_s32e:
2856 1.1 christos func = execute_s32e;
2857 1.1 christos break;
2858 1.1 christos case c0opc_rfwo: /* RFWO. */
2859 1.1 christos /* Here, we return from WindowOverflow handler and,
2860 1.1 christos if we stopped at the very beginning, which means
2861 1.1 christos A0 was saved, we have to restore it now. */
2862 1.1 christos if (a0_was_saved)
2863 1.1 christos {
2864 1.1 christos int arreg = arreg_number (gdbarch,
2865 1.1 christos gdbarch_tdep (gdbarch)->a0_base,
2866 1.1 christos wb);
2867 1.1 christos xtensa_write_register (arreg, a0_saved);
2868 1.1 christos }
2869 1.1 christos return xtWindowOverflow;
2870 1.1 christos case c0opc_rfwu: /* RFWU. */
2871 1.1 christos /* Here, we return from WindowUnderflow handler.
2872 1.1 christos Let's see if either A7 or A11 has to be restored. */
2873 1.1 christos if (WindowUnderflow12)
2874 1.1 christos {
2875 1.1 christos if (a11_was_saved)
2876 1.1 christos {
2877 1.1 christos int arreg = arreg_number (gdbarch,
2878 1.1 christos gdbarch_tdep (gdbarch)->a0_base + 11,
2879 1.1 christos wb);
2880 1.1 christos xtensa_write_register (arreg, a11_saved);
2881 1.1 christos }
2882 1.1 christos }
2883 1.1 christos else if (a7_was_saved)
2884 1.1 christos {
2885 1.1 christos int arreg = arreg_number (gdbarch,
2886 1.1 christos gdbarch_tdep (gdbarch)->a0_base + 7,
2887 1.1 christos wb);
2888 1.1 christos xtensa_write_register (arreg, a7_saved);
2889 1.1 christos }
2890 1.1 christos return xtWindowUnderflow;
2891 1.1 christos default: /* Simply skip this insns. */
2892 1.1 christos continue;
2893 1.1 christos }
2894 1.1 christos
2895 1.1 christos /* Decode arguments for L32E / S32E and simulate their execution. */
2896 1.1 christos if ( xtensa_opcode_num_operands (isa, opc) != 3 )
2897 1.1 christos return xtNoExceptionHandler;
2898 1.1 christos if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, &at))
2899 1.1 christos return xtNoExceptionHandler;
2900 1.1 christos if (xtensa_operand_decode (isa, opc, 0, &at))
2901 1.1 christos return xtNoExceptionHandler;
2902 1.1 christos if (xtensa_operand_get_field (isa, opc, 1, ifmt, is, slot, &as))
2903 1.1 christos return xtNoExceptionHandler;
2904 1.1 christos if (xtensa_operand_decode (isa, opc, 1, &as))
2905 1.1 christos return xtNoExceptionHandler;
2906 1.1 christos if (xtensa_operand_get_field (isa, opc, 2, ifmt, is, slot, &offset))
2907 1.1 christos return xtNoExceptionHandler;
2908 1.1 christos if (xtensa_operand_decode (isa, opc, 2, &offset))
2909 1.1 christos return xtNoExceptionHandler;
2910 1.1 christos
2911 1.1 christos (*func) (gdbarch, at, as, offset, wb);
2912 1.1 christos }
2913 1.1 christos
2914 1.1 christos ia += ilen;
2915 1.1 christos }
2916 1.1 christos return xtNoExceptionHandler;
2917 1.1 christos }
2918 1.1 christos
2919 1.1 christos /* Handle Window Overflow / Underflow exception frames. */
2920 1.1 christos
2921 1.1 christos static void
2922 1.1 christos xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
2923 1.1 christos xtensa_frame_cache_t *cache,
2924 1.1 christos CORE_ADDR pc)
2925 1.1 christos {
2926 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
2927 1.1 christos CORE_ADDR ps, wb, ws, ra;
2928 1.1 christos int epc1_regnum, i, regnum;
2929 1.1 christos xtensa_exception_handler_t eh_type;
2930 1.1 christos
2931 1.1 christos /* Read PS, WB, and WS from the hardware. Note that PS register
2932 1.1 christos must be present, if Windowed ABI is supported. */
2933 1.1 christos ps = xtensa_read_register (gdbarch_ps_regnum (gdbarch));
2934 1.1 christos wb = xtensa_read_register (gdbarch_tdep (gdbarch)->wb_regnum);
2935 1.1 christos ws = xtensa_read_register (gdbarch_tdep (gdbarch)->ws_regnum);
2936 1.1 christos
2937 1.1 christos /* Execute all the remaining instructions from Window Interrupt Handler
2938 1.1 christos by simulating them on the remote protocol level. On return, set the
2939 1.1 christos type of Xtensa Window Interrupt Handler, or report an error. */
2940 1.1 christos eh_type = execute_code (gdbarch, pc, wb);
2941 1.1 christos if (eh_type == xtNoExceptionHandler)
2942 1.1 christos error (_("\
2943 1.1 christos Unable to decode Xtensa Window Interrupt Handler's code."));
2944 1.1 christos
2945 1.1 christos cache->ps = ps ^ PS_EXC; /* Clear the exception bit in PS. */
2946 1.1 christos cache->call0 = 0; /* It's Windowed ABI. */
2947 1.1 christos
2948 1.1 christos /* All registers for the cached frame will be alive. */
2949 1.1 christos for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
2950 1.1 christos cache->wd.aregs[i] = -1;
2951 1.1 christos
2952 1.1 christos if (eh_type == xtWindowOverflow)
2953 1.1 christos cache->wd.ws = ws ^ (1 << wb);
2954 1.1 christos else /* eh_type == xtWindowUnderflow. */
2955 1.1 christos cache->wd.ws = ws | (1 << wb);
2956 1.1 christos
2957 1.1 christos cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB. */
2958 1.1 christos regnum = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base,
2959 1.1 christos cache->wd.wb);
2960 1.1 christos ra = xtensa_read_register (regnum);
2961 1.1 christos cache->wd.callsize = WINSIZE (ra);
2962 1.1 christos cache->prev_sp = xtensa_read_register (regnum + 1);
2963 1.1 christos /* Set regnum to a frame pointer of the frame being cached. */
2964 1.1 christos regnum = xtensa_scan_prologue (gdbarch, pc);
2965 1.1 christos regnum = arreg_number (gdbarch,
2966 1.1 christos gdbarch_tdep (gdbarch)->a0_base + regnum,
2967 1.1 christos cache->wd.wb);
2968 1.1 christos cache->base = get_frame_register_unsigned (this_frame, regnum);
2969 1.1 christos
2970 1.1 christos /* Read PC of interrupted function from EPC1 register. */
2971 1.1 christos epc1_regnum = xtensa_find_register_by_name (gdbarch,"epc1");
2972 1.1 christos if (epc1_regnum < 0)
2973 1.1 christos error(_("Unable to read Xtensa register EPC1"));
2974 1.1 christos cache->ra = xtensa_read_register (epc1_regnum);
2975 1.1 christos cache->pc = get_frame_func (this_frame);
2976 1.1 christos }
2977 1.1 christos
2978 1.1 christos
2979 1.1 christos /* Skip function prologue.
2980 1.1 christos
2981 1.1 christos Return the pc of the first instruction after prologue. GDB calls this to
2982 1.1 christos find the address of the first line of the function or (if there is no line
2983 1.1 christos number information) to skip the prologue for planting breakpoints on
2984 1.1 christos function entries. Use debug info (if present) or prologue analysis to skip
2985 1.1 christos the prologue to achieve reliable debugging behavior. For windowed ABI,
2986 1.1 christos only the 'entry' instruction is skipped. It is not strictly necessary to
2987 1.1 christos skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
2988 1.1 christos backtrace at any point in the prologue, however certain potential hazards
2989 1.1 christos are avoided and a more "normal" debugging experience is ensured by
2990 1.1 christos skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
2991 1.1 christos For example, if we don't skip the prologue:
2992 1.1 christos - Some args may not yet have been saved to the stack where the debug
2993 1.1 christos info expects to find them (true anyway when only 'entry' is skipped);
2994 1.1 christos - Software breakpoints ('break' instrs) may not have been unplanted
2995 1.1 christos when the prologue analysis is done on initializing the frame cache,
2996 1.1 christos and breaks in the prologue will throw off the analysis.
2997 1.1 christos
2998 1.1 christos If we have debug info ( line-number info, in particular ) we simply skip
2999 1.1 christos the code associated with the first function line effectively skipping
3000 1.1 christos the prologue code. It works even in cases like
3001 1.1 christos
3002 1.1 christos int main()
3003 1.1 christos { int local_var = 1;
3004 1.1 christos ....
3005 1.1 christos }
3006 1.1 christos
3007 1.1 christos because, for this source code, both Xtensa compilers will generate two
3008 1.1 christos separate entries ( with the same line number ) in dwarf line-number
3009 1.1 christos section to make sure there is a boundary between the prologue code and
3010 1.1 christos the rest of the function.
3011 1.1 christos
3012 1.1 christos If there is no debug info, we need to analyze the code. */
3013 1.1 christos
3014 1.1 christos /* #define DONT_SKIP_PROLOGUE */
3015 1.1 christos
3016 1.1 christos static CORE_ADDR
3017 1.1 christos xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
3018 1.1 christos {
3019 1.1 christos struct symtab_and_line prologue_sal;
3020 1.1 christos CORE_ADDR body_pc;
3021 1.1 christos
3022 1.1 christos DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
3023 1.1 christos
3024 1.1 christos #if DONT_SKIP_PROLOGUE
3025 1.1 christos return start_pc;
3026 1.1 christos #endif
3027 1.1 christos
3028 1.1 christos /* Try to find first body line from debug info. */
3029 1.1 christos
3030 1.1 christos prologue_sal = find_pc_line (start_pc, 0);
3031 1.1 christos if (prologue_sal.line != 0) /* Found debug info. */
3032 1.1 christos {
3033 1.1 christos /* In Call0, it is possible to have a function with only one instruction
3034 1.1 christos ('ret') resulting from a one-line optimized function that does nothing.
3035 1.1 christos In that case, prologue_sal.end may actually point to the start of the
3036 1.1 christos next function in the text section, causing a breakpoint to be set at
3037 1.1 christos the wrong place. Check, if the end address is within a different
3038 1.1 christos function, and if so return the start PC. We know we have symbol
3039 1.1 christos information. */
3040 1.1 christos
3041 1.1 christos CORE_ADDR end_func;
3042 1.1 christos
3043 1.1 christos if ((gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
3044 1.1 christos && call0_ret (start_pc, prologue_sal.end))
3045 1.1 christos return start_pc;
3046 1.1 christos
3047 1.1 christos find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
3048 1.1 christos if (end_func != start_pc)
3049 1.1 christos return start_pc;
3050 1.1 christos
3051 1.1 christos return prologue_sal.end;
3052 1.1 christos }
3053 1.1 christos
3054 1.1 christos /* No debug line info. Analyze prologue for Call0 or simply skip ENTRY. */
3055 1.1 christos body_pc = call0_analyze_prologue (gdbarch, start_pc, 0, 0,
3056 1.1 christos xtensa_alloc_frame_cache (0));
3057 1.1 christos return body_pc != 0 ? body_pc : start_pc;
3058 1.1 christos }
3059 1.1 christos
3060 1.1 christos /* Verify the current configuration. */
3061 1.1 christos static void
3062 1.1 christos xtensa_verify_config (struct gdbarch *gdbarch)
3063 1.1 christos {
3064 1.1 christos struct ui_file *log;
3065 1.1 christos struct cleanup *cleanups;
3066 1.1 christos struct gdbarch_tdep *tdep;
3067 1.1 christos long length;
3068 1.1 christos char *buf;
3069 1.1 christos
3070 1.1 christos tdep = gdbarch_tdep (gdbarch);
3071 1.1 christos log = mem_fileopen ();
3072 1.1 christos cleanups = make_cleanup_ui_file_delete (log);
3073 1.1 christos
3074 1.1 christos /* Verify that we got a reasonable number of AREGS. */
3075 1.1 christos if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
3076 1.1 christos fprintf_unfiltered (log, _("\
3077 1.1 christos \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
3078 1.1 christos tdep->num_aregs);
3079 1.1 christos
3080 1.1 christos /* Verify that certain registers exist. */
3081 1.1 christos
3082 1.1 christos if (tdep->pc_regnum == -1)
3083 1.1 christos fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
3084 1.1 christos if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
3085 1.1 christos fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));
3086 1.1 christos
3087 1.1 christos if (tdep->isa_use_windowed_registers)
3088 1.1 christos {
3089 1.1 christos if (tdep->wb_regnum == -1)
3090 1.1 christos fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
3091 1.1 christos if (tdep->ws_regnum == -1)
3092 1.1 christos fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
3093 1.1 christos if (tdep->ar_base == -1)
3094 1.1 christos fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
3095 1.1 christos }
3096 1.1 christos
3097 1.1 christos if (tdep->a0_base == -1)
3098 1.1 christos fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
3099 1.1 christos
3100 1.1 christos buf = ui_file_xstrdup (log, &length);
3101 1.1 christos make_cleanup (xfree, buf);
3102 1.1 christos if (length > 0)
3103 1.1 christos internal_error (__FILE__, __LINE__,
3104 1.1 christos _("the following are invalid: %s"), buf);
3105 1.1 christos do_cleanups (cleanups);
3106 1.1 christos }
3107 1.1 christos
3108 1.1 christos
3109 1.1 christos /* Derive specific register numbers from the array of registers. */
3110 1.1 christos
3111 1.1 christos static void
3112 1.1 christos xtensa_derive_tdep (struct gdbarch_tdep *tdep)
3113 1.1 christos {
3114 1.1 christos xtensa_register_t* rmap;
3115 1.1 christos int n, max_size = 4;
3116 1.1 christos
3117 1.1 christos tdep->num_regs = 0;
3118 1.1 christos tdep->num_nopriv_regs = 0;
3119 1.1 christos
3120 1.1 christos /* Special registers 0..255 (core). */
3121 1.1 christos #define XTENSA_DBREGN_SREG(n) (0x0200+(n))
3122 1.1 christos
3123 1.1 christos for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
3124 1.1 christos {
3125 1.1 christos if (rmap->target_number == 0x0020)
3126 1.1 christos tdep->pc_regnum = n;
3127 1.1 christos else if (rmap->target_number == 0x0100)
3128 1.1 christos tdep->ar_base = n;
3129 1.1 christos else if (rmap->target_number == 0x0000)
3130 1.1 christos tdep->a0_base = n;
3131 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
3132 1.1 christos tdep->wb_regnum = n;
3133 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
3134 1.1 christos tdep->ws_regnum = n;
3135 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
3136 1.1 christos tdep->debugcause_regnum = n;
3137 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
3138 1.1 christos tdep->exccause_regnum = n;
3139 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
3140 1.1 christos tdep->excvaddr_regnum = n;
3141 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
3142 1.1 christos tdep->lbeg_regnum = n;
3143 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
3144 1.1 christos tdep->lend_regnum = n;
3145 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
3146 1.1 christos tdep->lcount_regnum = n;
3147 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
3148 1.1 christos tdep->sar_regnum = n;
3149 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
3150 1.1 christos tdep->litbase_regnum = n;
3151 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
3152 1.1 christos tdep->ps_regnum = n;
3153 1.1 christos #if 0
3154 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
3155 1.1 christos tdep->interrupt_regnum = n;
3156 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
3157 1.1 christos tdep->interrupt2_regnum = n;
3158 1.1 christos else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
3159 1.1 christos tdep->cpenable_regnum = n;
3160 1.1 christos #endif
3161 1.1 christos
3162 1.1 christos if (rmap->byte_size > max_size)
3163 1.1 christos max_size = rmap->byte_size;
3164 1.1 christos if (rmap->mask != 0 && tdep->num_regs == 0)
3165 1.1 christos tdep->num_regs = n;
3166 1.1 christos /* Find out out how to deal with priveleged registers.
3167 1.1 christos
3168 1.1 christos if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3169 1.1 christos && tdep->num_nopriv_regs == 0)
3170 1.1 christos tdep->num_nopriv_regs = n;
3171 1.1 christos */
3172 1.1 christos if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
3173 1.1 christos && tdep->num_regs == 0)
3174 1.1 christos tdep->num_regs = n;
3175 1.1 christos }
3176 1.1 christos
3177 1.1 christos /* Number of pseudo registers. */
3178 1.1 christos tdep->num_pseudo_regs = n - tdep->num_regs;
3179 1.1 christos
3180 1.1 christos /* Empirically determined maximum sizes. */
3181 1.1 christos tdep->max_register_raw_size = max_size;
3182 1.1 christos tdep->max_register_virtual_size = max_size;
3183 1.1 christos }
3184 1.1 christos
3185 1.1 christos /* Module "constructor" function. */
3186 1.1 christos
3187 1.1 christos extern struct gdbarch_tdep xtensa_tdep;
3188 1.1 christos
3189 1.1 christos static struct gdbarch *
3190 1.1 christos xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3191 1.1 christos {
3192 1.1 christos struct gdbarch_tdep *tdep;
3193 1.1 christos struct gdbarch *gdbarch;
3194 1.1 christos
3195 1.1 christos DEBUGTRACE ("gdbarch_init()\n");
3196 1.1 christos
3197 1.1 christos /* We have to set the byte order before we call gdbarch_alloc. */
3198 1.1 christos info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
3199 1.1 christos
3200 1.1 christos tdep = &xtensa_tdep;
3201 1.1 christos gdbarch = gdbarch_alloc (&info, tdep);
3202 1.1 christos xtensa_derive_tdep (tdep);
3203 1.1 christos
3204 1.1 christos /* Verify our configuration. */
3205 1.1 christos xtensa_verify_config (gdbarch);
3206 1.1 christos xtensa_session_once_reported = 0;
3207 1.1 christos
3208 1.1 christos /* Pseudo-Register read/write. */
3209 1.1 christos set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
3210 1.1 christos set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
3211 1.1 christos
3212 1.1 christos /* Set target information. */
3213 1.1 christos set_gdbarch_num_regs (gdbarch, tdep->num_regs);
3214 1.1 christos set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
3215 1.1 christos set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
3216 1.1 christos set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
3217 1.1 christos set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
3218 1.1 christos
3219 1.1 christos /* Renumber registers for known formats (stabs and dwarf2). */
3220 1.1 christos set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
3221 1.1 christos set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
3222 1.1 christos
3223 1.1 christos /* We provide our own function to get register information. */
3224 1.1 christos set_gdbarch_register_name (gdbarch, xtensa_register_name);
3225 1.1 christos set_gdbarch_register_type (gdbarch, xtensa_register_type);
3226 1.1 christos
3227 1.1 christos /* To call functions from GDB using dummy frame. */
3228 1.1 christos set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
3229 1.1 christos
3230 1.1 christos set_gdbarch_believe_pcc_promotion (gdbarch, 1);
3231 1.1 christos
3232 1.1 christos set_gdbarch_return_value (gdbarch, xtensa_return_value);
3233 1.1 christos
3234 1.1 christos /* Advance PC across any prologue instructions to reach "real" code. */
3235 1.1 christos set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
3236 1.1 christos
3237 1.1 christos /* Stack grows downward. */
3238 1.1 christos set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3239 1.1 christos
3240 1.1 christos /* Set breakpoints. */
3241 1.1 christos set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
3242 1.1 christos
3243 1.1 christos /* After breakpoint instruction or illegal instruction, pc still
3244 1.1 christos points at break instruction, so don't decrement. */
3245 1.1 christos set_gdbarch_decr_pc_after_break (gdbarch, 0);
3246 1.1 christos
3247 1.1 christos /* We don't skip args. */
3248 1.1 christos set_gdbarch_frame_args_skip (gdbarch, 0);
3249 1.1 christos
3250 1.1 christos set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
3251 1.1 christos
3252 1.1 christos set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
3253 1.1 christos
3254 1.1 christos set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
3255 1.1 christos
3256 1.1 christos /* Frame handling. */
3257 1.1 christos frame_base_set_default (gdbarch, &xtensa_frame_base);
3258 1.1 christos frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
3259 1.1 christos dwarf2_append_unwinders (gdbarch);
3260 1.1 christos
3261 1.1 christos set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
3262 1.1 christos
3263 1.1 christos set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
3264 1.1 christos
3265 1.1 christos xtensa_add_reggroups (gdbarch);
3266 1.1 christos set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
3267 1.1 christos
3268 1.3 christos set_gdbarch_iterate_over_regset_sections
3269 1.3 christos (gdbarch, xtensa_iterate_over_regset_sections);
3270 1.1 christos
3271 1.1 christos set_solib_svr4_fetch_link_map_offsets
3272 1.1 christos (gdbarch, svr4_ilp32_fetch_link_map_offsets);
3273 1.1 christos
3274 1.6 christos /* Hook in the ABI-specific overrides, if they have been registered. */
3275 1.6 christos gdbarch_init_osabi (info, gdbarch);
3276 1.6 christos
3277 1.1 christos return gdbarch;
3278 1.1 christos }
3279 1.1 christos
3280 1.1 christos static void
3281 1.1 christos xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3282 1.1 christos {
3283 1.1 christos error (_("xtensa_dump_tdep(): not implemented"));
3284 1.1 christos }
3285 1.1 christos
3286 1.1 christos /* Provide a prototype to silence -Wmissing-prototypes. */
3287 1.1 christos extern initialize_file_ftype _initialize_xtensa_tdep;
3288 1.1 christos
3289 1.1 christos void
3290 1.1 christos _initialize_xtensa_tdep (void)
3291 1.1 christos {
3292 1.1 christos gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
3293 1.1 christos xtensa_init_reggroups ();
3294 1.1 christos
3295 1.1 christos add_setshow_zuinteger_cmd ("xtensa",
3296 1.1 christos class_maintenance,
3297 1.1 christos &xtensa_debug_level,
3298 1.1 christos _("Set Xtensa debugging."),
3299 1.1 christos _("Show Xtensa debugging."), _("\
3300 1.1 christos When non-zero, Xtensa-specific debugging is enabled. \
3301 1.1 christos Can be 1, 2, 3, or 4 indicating the level of debugging."),
3302 1.1 christos NULL,
3303 1.1 christos NULL,
3304 1.1 christos &setdebuglist, &showdebuglist);
3305 1.1 christos }
3306