Update trap/break handling in nios2 prologue analyzer.
[deliverable/binutils-gdb.git] / gdb / nios2-tdep.c
CommitLineData
a1217d97 1/* Target-machine dependent code for Nios II, for GDB.
32d0add0 2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
a1217d97
SL
3 Contributed by Peter Brookes (pbrookes@altera.com)
4 and Andrew Draper (adraper@altera.com).
5 Contributed by Mentor Graphics, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "defs.h"
23#include "frame.h"
24#include "frame-unwind.h"
25#include "frame-base.h"
26#include "trad-frame.h"
27#include "dwarf2-frame.h"
28#include "symtab.h"
29#include "inferior.h"
30#include "gdbtypes.h"
31#include "gdbcore.h"
32#include "gdbcmd.h"
33#include "osabi.h"
34#include "target.h"
35#include "dis-asm.h"
36#include "regcache.h"
37#include "value.h"
38#include "symfile.h"
39#include "arch-utils.h"
40#include "floatformat.h"
a1217d97
SL
41#include "infcall.h"
42#include "regset.h"
43#include "target-descriptions.h"
44
45/* To get entry_point_address. */
46#include "objfiles.h"
47
48/* Nios II ISA specific encodings and macros. */
49#include "opcode/nios2.h"
50
51/* Nios II specific header. */
52#include "nios2-tdep.h"
53
54#include "features/nios2.c"
55
56/* Control debugging information emitted in this file. */
57
58static int nios2_debug = 0;
59
60/* The following structures are used in the cache for prologue
61 analysis; see the reg_value and reg_saved tables in
62 struct nios2_unwind_cache, respectively. */
63
64/* struct reg_value is used to record that a register has the same value
65 as reg at the given offset from the start of a function. */
66
67struct reg_value
68{
69 int reg;
70 unsigned int offset;
71};
72
73/* struct reg_saved is used to record that a register value has been saved at
74 basereg + addr, for basereg >= 0. If basereg < 0, that indicates
75 that the register is not known to have been saved. Note that when
76 basereg == NIOS2_Z_REGNUM (that is, r0, which holds value 0),
77 addr is an absolute address. */
78
79struct reg_saved
80{
81 int basereg;
82 CORE_ADDR addr;
83};
84
85struct nios2_unwind_cache
86{
87 /* The frame's base, optionally used by the high-level debug info. */
88 CORE_ADDR base;
89
90 /* The previous frame's inner most stack address. Used as this
91 frame ID's stack_addr. */
92 CORE_ADDR cfa;
93
94 /* The address of the first instruction in this function. */
95 CORE_ADDR pc;
96
97 /* Which register holds the return address for the frame. */
98 int return_regnum;
99
100 /* Table indicating what changes have been made to each register. */
101 struct reg_value reg_value[NIOS2_NUM_REGS];
102
103 /* Table indicating where each register has been saved. */
104 struct reg_saved reg_saved[NIOS2_NUM_REGS];
105};
106
107
108/* This array is a mapping from Dwarf-2 register numbering to GDB's. */
109
110static int nios2_dwarf2gdb_regno_map[] =
111{
112 0, 1, 2, 3,
113 4, 5, 6, 7,
114 8, 9, 10, 11,
115 12, 13, 14, 15,
116 16, 17, 18, 19,
117 20, 21, 22, 23,
118 24, 25,
119 NIOS2_GP_REGNUM, /* 26 */
120 NIOS2_SP_REGNUM, /* 27 */
121 NIOS2_FP_REGNUM, /* 28 */
122 NIOS2_EA_REGNUM, /* 29 */
123 NIOS2_BA_REGNUM, /* 30 */
124 NIOS2_RA_REGNUM, /* 31 */
125 NIOS2_PC_REGNUM, /* 32 */
126 NIOS2_STATUS_REGNUM, /* 33 */
127 NIOS2_ESTATUS_REGNUM, /* 34 */
128 NIOS2_BSTATUS_REGNUM, /* 35 */
129 NIOS2_IENABLE_REGNUM, /* 36 */
130 NIOS2_IPENDING_REGNUM, /* 37 */
131 NIOS2_CPUID_REGNUM, /* 38 */
132 39, /* CTL6 */ /* 39 */
133 NIOS2_EXCEPTION_REGNUM, /* 40 */
134 NIOS2_PTEADDR_REGNUM, /* 41 */
135 NIOS2_TLBACC_REGNUM, /* 42 */
136 NIOS2_TLBMISC_REGNUM, /* 43 */
137 NIOS2_ECCINJ_REGNUM, /* 44 */
138 NIOS2_BADADDR_REGNUM, /* 45 */
139 NIOS2_CONFIG_REGNUM, /* 46 */
140 NIOS2_MPUBASE_REGNUM, /* 47 */
141 NIOS2_MPUACC_REGNUM /* 48 */
142};
143
144
145/* Implement the dwarf2_reg_to_regnum gdbarch method. */
146
147static int
148nios2_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
149{
150 if (dw_reg < 0 || dw_reg > NIOS2_NUM_REGS)
151 {
1e52bda6 152 warning (_("Dwarf-2 uses unmapped register #%d"), dw_reg);
a1217d97
SL
153 return dw_reg;
154 }
155
156 return nios2_dwarf2gdb_regno_map[dw_reg];
157}
158
159/* Canonical names for the 49 registers. */
160
161static const char *const nios2_reg_names[NIOS2_NUM_REGS] =
162{
163 "zero", "at", "r2", "r3", "r4", "r5", "r6", "r7",
164 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
165 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
0b76b0ce 166 "et", "bt", "gp", "sp", "fp", "ea", "sstatus", "ra",
a1217d97
SL
167 "pc",
168 "status", "estatus", "bstatus", "ienable",
169 "ipending", "cpuid", "ctl6", "exception",
170 "pteaddr", "tlbacc", "tlbmisc", "eccinj",
171 "badaddr", "config", "mpubase", "mpuacc"
172};
173
174/* Implement the register_name gdbarch method. */
175
176static const char *
177nios2_register_name (struct gdbarch *gdbarch, int regno)
178{
179 /* Use mnemonic aliases for GPRs. */
180 if (regno >= 0 && regno < NIOS2_NUM_REGS)
181 return nios2_reg_names[regno];
182 else
183 return tdesc_register_name (gdbarch, regno);
184}
185
186/* Implement the register_type gdbarch method. */
187
188static struct type *
189nios2_register_type (struct gdbarch *gdbarch, int regno)
190{
191 /* If the XML description has register information, use that to
192 determine the register type. */
193 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
194 return tdesc_register_type (gdbarch, regno);
195
196 if (regno == NIOS2_PC_REGNUM)
197 return builtin_type (gdbarch)->builtin_func_ptr;
198 else if (regno == NIOS2_SP_REGNUM)
199 return builtin_type (gdbarch)->builtin_data_ptr;
200 else
201 return builtin_type (gdbarch)->builtin_uint32;
202}
203
204/* Given a return value in REGCACHE with a type VALTYPE,
205 extract and copy its value into VALBUF. */
206
207static void
208nios2_extract_return_value (struct gdbarch *gdbarch, struct type *valtype,
209 struct regcache *regcache, gdb_byte *valbuf)
210{
211 int len = TYPE_LENGTH (valtype);
212
213 /* Return values of up to 8 bytes are returned in $r2 $r3. */
214 if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
215 regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
216 else
217 {
218 gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
219 + register_size (gdbarch, NIOS2_R3_REGNUM)));
220 regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
221 regcache_cooked_read (regcache, NIOS2_R3_REGNUM, valbuf + 4);
222 }
223}
224
225/* Write into appropriate registers a function return value
226 of type TYPE, given in virtual format. */
227
228static void
229nios2_store_return_value (struct gdbarch *gdbarch, struct type *valtype,
230 struct regcache *regcache, const gdb_byte *valbuf)
231{
232 int len = TYPE_LENGTH (valtype);
233
234 /* Return values of up to 8 bytes are returned in $r2 $r3. */
235 if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
236 regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
237 else
238 {
239 gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
240 + register_size (gdbarch, NIOS2_R3_REGNUM)));
241 regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
242 regcache_cooked_write (regcache, NIOS2_R3_REGNUM, valbuf + 4);
243 }
244}
245
246
247/* Set up the default values of the registers. */
248
249static void
250nios2_setup_default (struct nios2_unwind_cache *cache)
251{
252 int i;
253
254 for (i = 0; i < NIOS2_NUM_REGS; i++)
255 {
256 /* All registers start off holding their previous values. */
257 cache->reg_value[i].reg = i;
258 cache->reg_value[i].offset = 0;
259
260 /* All registers start off not saved. */
261 cache->reg_saved[i].basereg = -1;
262 cache->reg_saved[i].addr = 0;
263 }
264}
265
266/* Initialize the unwind cache. */
267
268static void
269nios2_init_cache (struct nios2_unwind_cache *cache, CORE_ADDR pc)
270{
271 cache->base = 0;
272 cache->cfa = 0;
273 cache->pc = pc;
274 cache->return_regnum = NIOS2_RA_REGNUM;
275 nios2_setup_default (cache);
276}
277
d53c26c7
SL
278/* Read and identify an instruction at PC. If INSNP is non-null,
279 store the instruction word into that location. Return the opcode
280 pointer or NULL if the memory couldn't be read or disassembled. */
281
282static const struct nios2_opcode *
283nios2_fetch_insn (struct gdbarch *gdbarch, CORE_ADDR pc,
284 unsigned int *insnp)
285{
286 LONGEST memword;
287 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
288 unsigned int insn;
289
290 if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
291 gdbarch_byte_order (gdbarch), &memword))
292 return NULL;
293
294 insn = (unsigned int) memword;
295 if (insnp)
296 *insnp = insn;
297 return nios2_find_opcode_hash (insn, mach);
298}
299
300
301/* Match and disassemble an ADD-type instruction, with 3 register operands.
302 Returns true on success, and fills in the operand pointers. */
303
304static int
305nios2_match_add (uint32_t insn, const struct nios2_opcode *op,
306 unsigned long mach, int *ra, int *rb, int *rc)
307{
308 if (op->match == MATCH_R1_ADD || op->match == MATCH_R1_MOV)
309 {
310 *ra = GET_IW_R_A (insn);
311 *rb = GET_IW_R_B (insn);
312 *rc = GET_IW_R_C (insn);
313 return 1;
314 }
315 return 0;
316}
317
318/* Match and disassemble a SUB-type instruction, with 3 register operands.
319 Returns true on success, and fills in the operand pointers. */
320
321static int
322nios2_match_sub (uint32_t insn, const struct nios2_opcode *op,
323 unsigned long mach, int *ra, int *rb, int *rc)
324{
325 if (op->match == MATCH_R1_SUB)
326 {
327 *ra = GET_IW_R_A (insn);
328 *rb = GET_IW_R_B (insn);
329 *rc = GET_IW_R_C (insn);
330 return 1;
331 }
332 return 0;
333}
334
335/* Match and disassemble an ADDI-type instruction, with 2 register operands
336 and one immediate operand.
337 Returns true on success, and fills in the operand pointers. */
338
339static int
340nios2_match_addi (uint32_t insn, const struct nios2_opcode *op,
341 unsigned long mach, int *ra, int *rb, int *imm)
342{
343 if (op->match == MATCH_R1_ADDI)
344 {
345 *ra = GET_IW_I_A (insn);
346 *rb = GET_IW_I_B (insn);
347 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
348 return 1;
349 }
350 return 0;
351}
352
353/* Match and disassemble an ORHI-type instruction, with 2 register operands
354 and one unsigned immediate operand.
355 Returns true on success, and fills in the operand pointers. */
356
357static int
358nios2_match_orhi (uint32_t insn, const struct nios2_opcode *op,
359 unsigned long mach, int *ra, int *rb, unsigned int *uimm)
360{
361 if (op->match == MATCH_R1_ORHI)
362 {
363 *ra = GET_IW_I_A (insn);
364 *rb = GET_IW_I_B (insn);
365 *uimm = GET_IW_I_IMM16 (insn);
366 return 1;
367 }
368 return 0;
369}
370
371/* Match and disassemble a STW-type instruction, with 2 register operands
372 and one immediate operand.
373 Returns true on success, and fills in the operand pointers. */
374
375static int
376nios2_match_stw (uint32_t insn, const struct nios2_opcode *op,
377 unsigned long mach, int *ra, int *rb, int *imm)
378{
379 if (op->match == MATCH_R1_STW || op->match == MATCH_R1_STWIO)
380 {
381 *ra = GET_IW_I_A (insn);
382 *rb = GET_IW_I_B (insn);
383 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
384 return 1;
385 }
386 return 0;
387}
388
389/* Match and disassemble a LDW-type instruction, with 2 register operands
390 and one immediate operand.
391 Returns true on success, and fills in the operand pointers. */
392
393static int
394nios2_match_ldw (uint32_t insn, const struct nios2_opcode *op,
395 unsigned long mach, int *ra, int *rb, int *imm)
396{
397 if (op->match == MATCH_R1_LDW || op->match == MATCH_R1_LDWIO)
398 {
399 *ra = GET_IW_I_A (insn);
400 *rb = GET_IW_I_B (insn);
401 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
402 return 1;
403 }
404 return 0;
405}
406
407/* Match and disassemble a RDCTL instruction, with 2 register operands.
408 Returns true on success, and fills in the operand pointers. */
409
410static int
411nios2_match_rdctl (uint32_t insn, const struct nios2_opcode *op,
412 unsigned long mach, int *ra, int *rc)
413{
414 if (op->match == MATCH_R1_RDCTL)
415 {
416 *ra = GET_IW_R_IMM5 (insn);
417 *rc = GET_IW_R_C (insn);
418 return 1;
419 }
420 return 0;
421}
422
423
424/* Match and disassemble a branch instruction, with (potentially)
425 2 register operands and one immediate operand.
426 Returns true on success, and fills in the operand pointers. */
427
428enum branch_condition {
429 branch_none,
430 branch_eq,
431 branch_ne,
432 branch_ge,
433 branch_geu,
434 branch_lt,
435 branch_ltu
436};
437
438static int
439nios2_match_branch (uint32_t insn, const struct nios2_opcode *op,
440 unsigned long mach, int *ra, int *rb, int *imm,
441 enum branch_condition *cond)
442{
443 switch (op->match)
444 {
445 case MATCH_R1_BR:
446 *cond = branch_none;
447 break;
448 case MATCH_R1_BEQ:
449 *cond = branch_eq;
450 break;
451 case MATCH_R1_BNE:
452 *cond = branch_ne;
453 break;
454 case MATCH_R1_BGE:
455 *cond = branch_ge;
456 break;
457 case MATCH_R1_BGEU:
458 *cond = branch_geu;
459 break;
460 case MATCH_R1_BLT:
461 *cond = branch_lt;
462 break;
463 case MATCH_R1_BLTU:
464 *cond = branch_ltu;
465 break;
466 default:
467 return 0;
468 }
469 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
470 *ra = GET_IW_I_A (insn);
471 *rb = GET_IW_I_B (insn);
472 return 1;
473}
474
475/* Match and disassemble a direct jump instruction, with an
476 unsigned operand. Returns true on success, and fills in the operand
477 pointer. */
478
479static int
480nios2_match_jmpi (uint32_t insn, const struct nios2_opcode *op,
481 unsigned long mach, unsigned int *uimm)
482{
483 if (op->match == MATCH_R1_JMPI)
484 {
485 *uimm = GET_IW_J_IMM26 (insn) << 2;
486 return 1;
487 }
488 return 0;
489}
490
491/* Match and disassemble a direct call instruction, with an
492 unsigned operand. Returns true on success, and fills in the operand
493 pointer. */
494
495static int
496nios2_match_calli (uint32_t insn, const struct nios2_opcode *op,
497 unsigned long mach, unsigned int *uimm)
498{
499 if (op->match == MATCH_R1_CALL)
500 {
501 *uimm = GET_IW_J_IMM26 (insn) << 2;
502 return 1;
503 }
504 return 0;
505}
506
507/* Match and disassemble an indirect jump instruction, with a
508 (possibly implicit) register operand. Returns true on success, and fills
509 in the operand pointer. */
510
511static int
512nios2_match_jmpr (uint32_t insn, const struct nios2_opcode *op,
513 unsigned long mach, int *ra)
514{
515 switch (op->match)
516 {
517 case MATCH_R1_JMP:
518 *ra = GET_IW_I_A (insn);
519 return 1;
520 case MATCH_R1_RET:
521 *ra = NIOS2_RA_REGNUM;
522 return 1;
523 case MATCH_R1_ERET:
524 *ra = NIOS2_EA_REGNUM;
525 return 1;
526 case MATCH_R1_BRET:
527 *ra = NIOS2_BA_REGNUM;
528 return 1;
529 default:
530 return 0;
531 }
532}
533
534/* Match and disassemble an indirect call instruction, with a register
535 operand. Returns true on success, and fills in the operand pointer. */
536
537static int
538nios2_match_callr (uint32_t insn, const struct nios2_opcode *op,
539 unsigned long mach, int *ra)
540{
541 if (op->match == MATCH_R1_CALLR)
542 {
543 *ra = GET_IW_I_A (insn);
544 return 1;
545 }
546 return 0;
547}
548
549/* Match and disassemble a break instruction, with an unsigned operand.
550 Returns true on success, and fills in the operand pointer. */
551
552static int
553nios2_match_break (uint32_t insn, const struct nios2_opcode *op,
554 unsigned long mach, unsigned int *uimm)
555{
556 if (op->match == MATCH_R1_BREAK)
557 {
558 *uimm = GET_IW_R_IMM5 (insn);
559 return 1;
560 }
561 return 0;
562}
563
564/* Match and disassemble a trap instruction, with an unsigned operand.
565 Returns true on success, and fills in the operand pointer. */
566
567static int
568nios2_match_trap (uint32_t insn, const struct nios2_opcode *op,
569 unsigned long mach, unsigned int *uimm)
570{
571 if (op->match == MATCH_R1_TRAP)
572 {
573 *uimm = GET_IW_R_IMM5 (insn);
574 return 1;
575 }
576 return 0;
577}
578
a1217d97
SL
579/* Helper function to identify when we're in a function epilogue;
580 that is, the part of the function from the point at which the
d53c26c7
SL
581 stack adjustments are made, to the return or sibcall.
582 Note that we may have several stack adjustment instructions, and
583 this function needs to test whether the stack teardown has already
584 started before current_pc, not whether it has completed. */
a1217d97
SL
585
586static int
587nios2_in_epilogue_p (struct gdbarch *gdbarch,
588 CORE_ADDR current_pc,
589 CORE_ADDR start_pc)
590{
d53c26c7 591 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
7f1659b5
SL
592 /* Maximum number of possibly-epilogue instructions to check.
593 Note that this number should not be too large, else we can
594 potentially end up iterating through unmapped memory. */
595 int ninsns, max_insns = 5;
d53c26c7
SL
596 unsigned int insn;
597 const struct nios2_opcode *op = NULL;
598 unsigned int uimm;
599 int imm;
600 int ra, rb, rc;
601 enum branch_condition cond;
7f1659b5 602 CORE_ADDR pc;
a1217d97
SL
603
604 /* There has to be a previous instruction in the function. */
7f1659b5
SL
605 if (current_pc <= start_pc)
606 return 0;
607
608 /* Find the previous instruction before current_pc.
609 For the moment we will assume that all instructions are the
610 same size here. */
611 pc = current_pc - NIOS2_OPCODE_SIZE;
d53c26c7 612
7f1659b5
SL
613 /* Beginning with the previous instruction we just located, check whether
614 we are in a sequence of at least one stack adjustment instruction.
615 Possible instructions here include:
d53c26c7
SL
616 ADDI sp, sp, n
617 ADD sp, sp, rn
618 LDW sp, n(sp) */
7f1659b5
SL
619 for (ninsns = 0; ninsns < max_insns; ninsns++)
620 {
621 int ok = 0;
622
623 /* Fetch the insn at pc. */
624 op = nios2_fetch_insn (gdbarch, pc, &insn);
d53c26c7
SL
625 if (op == NULL)
626 return 0;
7f1659b5 627 pc += op->size;
d53c26c7
SL
628
629 /* Was it a stack adjustment? */
630 if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
631 ok = (rb == NIOS2_SP_REGNUM);
632 else if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
633 ok = (rc == NIOS2_SP_REGNUM);
634 else if (nios2_match_ldw (insn, op, mach, &ra, &rb, &imm))
635 ok = (rb == NIOS2_SP_REGNUM);
636 if (!ok)
7f1659b5 637 break;
a1217d97 638 }
7f1659b5
SL
639
640 /* No stack adjustments found. */
641 if (ninsns == 0)
642 return 0;
643
644 /* We found more stack adjustments than we expect GCC to be generating.
645 Since it looks like a stack unwind might be in progress tell GDB to
646 treat it as such. */
647 if (ninsns == max_insns)
648 return 1;
649
650 /* The next instruction following the stack adjustments must be a
651 return, jump, or unconditional branch. */
652 if (nios2_match_jmpr (insn, op, mach, &ra)
653 || nios2_match_jmpi (insn, op, mach, &uimm)
654 || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
655 && cond == branch_none))
656 return 1;
657
a1217d97
SL
658 return 0;
659}
660
c9cf6e20 661/* Implement the stack_frame_destroyed_p gdbarch method. */
a1217d97
SL
662
663static int
c9cf6e20 664nios2_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
a1217d97
SL
665{
666 CORE_ADDR func_addr;
667
668 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
669 return nios2_in_epilogue_p (gdbarch, pc, func_addr);
670
671 return 0;
672}
673
a1217d97
SL
674/* Do prologue analysis, returning the PC of the first instruction
675 after the function prologue. Assumes CACHE has already been
676 initialized. THIS_FRAME can be null, in which case we are only
677 interested in skipping the prologue. Otherwise CACHE is filled in
678 from the frame information.
679
d53c26c7
SL
680 The prologue may consist of the following parts:
681 1) Profiling instrumentation. For non-PIC code it looks like:
a1217d97
SL
682 mov r8, ra
683 call mcount
684 mov ra, r8
685
d53c26c7
SL
686 2) A stack adjustment and save of R4-R7 for varargs functions.
687 This is typically merged with item 3.
688
689 3) A stack adjustment and save of the callee-saved registers;
690 typically an explicit SP decrement and individual register
691 saves.
692
693 There may also be a stack switch here in an exception handler
694 in place of a stack adjustment. It looks like:
a1217d97
SL
695 movhi rx, %hiadj(newstack)
696 addhi rx, rx, %lo(newstack)
697 stw sp, constant(rx)
698 mov sp, rx
699
9aaf8e3a 700 4) A frame pointer save, which can be either a MOV or ADDI.
d53c26c7 701
9aaf8e3a
SL
702 5) A further stack pointer adjustment. This is normally included
703 adjustment in step 3 unless the total adjustment is too large
d53c26c7
SL
704 to be done in one step.
705
706 7) A stack overflow check, which can take either of these forms:
a1217d97 707 bgeu sp, rx, +8
9aaf8e3a 708 trap 3
a1217d97
SL
709 or
710 bltu sp, rx, .Lstack_overflow
711 ...
712 .Lstack_overflow:
9aaf8e3a
SL
713 trap 3
714
715 Older versions of GCC emitted "break 3" instead of "trap 3" here,
716 so we check for both cases.
717
718 Older GCC versions emitted stack overflow checks after the SP
719 adjustments in both steps 3 and 4. Starting with GCC 6, there is
720 at most one overflow check, which is placed before the first
721 stack adjustment for R2 CDX and after the first stack adjustment
722 otherwise.
a1217d97 723
d53c26c7
SL
724 The prologue instructions may be combined or interleaved with other
725 instructions.
a1217d97
SL
726
727 To cope with all this variability we decode all the instructions
d53c26c7
SL
728 from the start of the prologue until we hit an instruction that
729 cannot possibly be a prologue instruction, such as a branch, call,
730 return, or epilogue instruction. The prologue is considered to end
731 at the last instruction that can definitely be considered a
732 prologue instruction. */
a1217d97
SL
733
734static CORE_ADDR
735nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc,
736 const CORE_ADDR current_pc,
737 struct nios2_unwind_cache *cache,
738 struct frame_info *this_frame)
739{
d53c26c7 740 /* Maximum number of possibly-prologue instructions to check.
a1217d97
SL
741 Note that this number should not be too large, else we can
742 potentially end up iterating through unmapped memory. */
d53c26c7 743 int ninsns, max_insns = 50;
a1217d97
SL
744 int regno;
745 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
d53c26c7 746 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
a1217d97
SL
747
748 /* Does the frame set up the FP register? */
749 int base_reg = 0;
750
751 struct reg_value *value = cache->reg_value;
752 struct reg_value temp_value[NIOS2_NUM_REGS];
753
754 int i;
755
756 /* Save the starting PC so we can correct the pc after running
757 through the prolog, using symbol info. */
758 CORE_ADDR pc = start_pc;
759
760 /* Is this an exception handler? */
761 int exception_handler = 0;
762
763 /* What was the original value of SP (or fake original value for
764 functions which switch stacks? */
765 CORE_ADDR frame_high;
766
d53c26c7 767 /* The last definitely-prologue instruction seen. */
a1217d97
SL
768 CORE_ADDR prologue_end;
769
770 /* Is this the innermost function? */
771 int innermost = (this_frame ? (frame_relative_level (this_frame) == 0) : 1);
772
773 if (nios2_debug)
774 fprintf_unfiltered (gdb_stdlog,
775 "{ nios2_analyze_prologue start=%s, current=%s ",
776 paddress (gdbarch, start_pc),
777 paddress (gdbarch, current_pc));
778
779 /* Set up the default values of the registers. */
780 nios2_setup_default (cache);
a1217d97
SL
781
782 /* Find the prologue instructions. */
d53c26c7
SL
783 prologue_end = start_pc;
784 for (ninsns = 0; ninsns < max_insns; ninsns++)
a1217d97
SL
785 {
786 /* Present instruction. */
787 uint32_t insn;
d53c26c7
SL
788 const struct nios2_opcode *op;
789 int ra, rb, rc, imm;
790 unsigned int uimm;
791 unsigned int reglist;
792 int wb, ret;
793 enum branch_condition cond;
a1217d97
SL
794
795 if (pc == current_pc)
796 {
797 /* When we reach the current PC we must save the current
798 register state (for the backtrace) but keep analysing
799 because there might be more to find out (eg. is this an
800 exception handler). */
801 memcpy (temp_value, value, sizeof (temp_value));
802 value = temp_value;
803 if (nios2_debug)
804 fprintf_unfiltered (gdb_stdlog, "*");
805 }
806
d53c26c7
SL
807 op = nios2_fetch_insn (gdbarch, pc, &insn);
808
809 /* Unknown opcode? Stop scanning. */
810 if (op == NULL)
811 break;
812 pc += op->size;
a1217d97
SL
813
814 if (nios2_debug)
815 fprintf_unfiltered (gdb_stdlog, "[%08X]", insn);
816
817 /* The following instructions can appear in the prologue. */
818
d53c26c7 819 if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
a1217d97
SL
820 {
821 /* ADD rc, ra, rb (also used for MOV) */
a1217d97
SL
822 if (rc == NIOS2_SP_REGNUM
823 && rb == 0
824 && value[ra].reg == cache->reg_saved[NIOS2_SP_REGNUM].basereg)
825 {
826 /* If the previous value of SP is available somewhere
827 near the new stack pointer value then this is a
828 stack switch. */
829
830 /* If any registers were saved on the stack before then
831 we can't backtrace into them now. */
832 for (i = 0 ; i < NIOS2_NUM_REGS ; i++)
833 {
834 if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
835 cache->reg_saved[i].basereg = -1;
836 if (value[i].reg == NIOS2_SP_REGNUM)
837 value[i].reg = -1;
838 }
839
840 /* Create a fake "high water mark" 4 bytes above where SP
841 was stored and fake up the registers to be consistent
842 with that. */
843 value[NIOS2_SP_REGNUM].reg = NIOS2_SP_REGNUM;
844 value[NIOS2_SP_REGNUM].offset
845 = (value[ra].offset
846 - cache->reg_saved[NIOS2_SP_REGNUM].addr
847 - 4);
848 cache->reg_saved[NIOS2_SP_REGNUM].basereg = NIOS2_SP_REGNUM;
849 cache->reg_saved[NIOS2_SP_REGNUM].addr = -4;
850 }
851
aa489395
SL
852 else if (rc == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
853 /* This is setting SP from FP. This only happens in the
854 function epilogue. */
855 break;
856
a1217d97
SL
857 else if (rc != 0)
858 {
859 if (value[rb].reg == 0)
860 value[rc].reg = value[ra].reg;
861 else if (value[ra].reg == 0)
862 value[rc].reg = value[rb].reg;
863 else
864 value[rc].reg = -1;
865 value[rc].offset = value[ra].offset + value[rb].offset;
866 }
a1217d97 867
aa489395
SL
868 /* The add/move is only considered a prologue instruction
869 if the destination is SP or FP. */
870 if (rc == NIOS2_SP_REGNUM || rc == NIOS2_FP_REGNUM)
871 prologue_end = pc;
d53c26c7
SL
872 }
873
874 else if (nios2_match_sub (insn, op, mach, &ra, &rb, &rc))
a1217d97
SL
875 {
876 /* SUB rc, ra, rb */
aa489395
SL
877 if (rc == NIOS2_SP_REGNUM && rb == NIOS2_SP_REGNUM
878 && value[rc].reg != 0)
879 /* If we are decrementing the SP by a non-constant amount,
880 this is alloca, not part of the prologue. */
881 break;
882 else if (rc != 0)
a1217d97
SL
883 {
884 if (value[rb].reg == 0)
885 value[rc].reg = value[ra].reg;
886 else
887 value[rc].reg = -1;
888 value[rc].offset = value[ra].offset - value[rb].offset;
889 }
890 }
891
d53c26c7 892 else if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
a1217d97 893 {
d53c26c7 894 /* ADDI rb, ra, imm */
a1217d97 895
aa489395 896 /* A positive stack adjustment has to be part of the epilogue. */
a1217d97 897 if (rb == NIOS2_SP_REGNUM
aa489395
SL
898 && (imm > 0 || value[ra].reg != NIOS2_SP_REGNUM))
899 break;
900
901 /* Likewise restoring SP from FP. */
902 else if (rb == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
a1217d97
SL
903 break;
904
905 if (rb != 0)
906 {
907 value[rb].reg = value[ra].reg;
d53c26c7 908 value[rb].offset = value[ra].offset + imm;
a1217d97
SL
909 }
910
aa489395
SL
911 /* The add is only considered a prologue instruction
912 if the destination is SP or FP. */
913 if (rb == NIOS2_SP_REGNUM || rb == NIOS2_FP_REGNUM)
914 prologue_end = pc;
a1217d97
SL
915 }
916
d53c26c7 917 else if (nios2_match_orhi (insn, op, mach, &ra, &rb, &uimm))
a1217d97 918 {
d53c26c7 919 /* ORHI rb, ra, uimm (also used for MOVHI) */
a1217d97
SL
920 if (rb != 0)
921 {
922 value[rb].reg = (value[ra].reg == 0) ? 0 : -1;
d53c26c7 923 value[rb].offset = value[ra].offset | (uimm << 16);
a1217d97
SL
924 }
925 }
926
d53c26c7 927 else if (nios2_match_stw (insn, op, mach, &ra, &rb, &imm))
a1217d97 928 {
d53c26c7 929 /* STW rb, imm(ra) */
a1217d97 930
d53c26c7 931 /* Are we storing the original value of a register to the stack?
a1217d97
SL
932 For exception handlers the value of EA-4 (return
933 address from interrupts etc) is sometimes stored. */
934 int orig = value[rb].reg;
935 if (orig > 0
936 && (value[rb].offset == 0
d53c26c7
SL
937 || (orig == NIOS2_EA_REGNUM && value[rb].offset == -4))
938 && ((value[ra].reg == NIOS2_SP_REGNUM
a1217d97 939 && cache->reg_saved[orig].basereg != NIOS2_SP_REGNUM)
d53c26c7
SL
940 || cache->reg_saved[orig].basereg == -1))
941 {
942 if (pc < current_pc)
a1217d97 943 {
d53c26c7
SL
944 /* Save off callee saved registers. */
945 cache->reg_saved[orig].basereg = value[ra].reg;
946 cache->reg_saved[orig].addr = value[ra].offset + imm;
a1217d97 947 }
d53c26c7
SL
948
949 prologue_end = pc;
950
951 if (orig == NIOS2_EA_REGNUM || orig == NIOS2_ESTATUS_REGNUM)
952 exception_handler = 1;
a1217d97
SL
953 }
954 else
d53c26c7
SL
955 /* Non-stack memory writes cannot appear in the prologue. */
956 break;
a1217d97
SL
957 }
958
d53c26c7 959 else if (nios2_match_rdctl (insn, op, mach, &ra, &rc))
a1217d97 960 {
d53c26c7
SL
961 /* RDCTL rC, ctlN
962 This can appear in exception handlers in combination with
963 a subsequent save to the stack frame. */
a1217d97
SL
964 if (rc != 0)
965 {
d53c26c7 966 value[rc].reg = NIOS2_STATUS_REGNUM + ra;
a1217d97
SL
967 value[rc].offset = 0;
968 }
a1217d97
SL
969 }
970
d53c26c7 971 else if (nios2_match_calli (insn, op, mach, &uimm))
a1217d97 972 {
d53c26c7
SL
973 if (value[8].reg == NIOS2_RA_REGNUM
974 && value[8].offset == 0
975 && value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM
976 && value[NIOS2_SP_REGNUM].offset == 0)
977 {
978 /* A CALL instruction. This is treated as a call to mcount
979 if ra has been stored into r8 beforehand and if it's
980 before the stack adjust.
981 Note mcount corrupts r2-r3, r9-r15 & ra. */
982 for (i = 2 ; i <= 3 ; i++)
983 value[i].reg = -1;
984 for (i = 9 ; i <= 15 ; i++)
985 value[i].reg = -1;
986 value[NIOS2_RA_REGNUM].reg = -1;
987
988 prologue_end = pc;
989 }
a1217d97 990
d53c26c7 991 /* Other calls are not part of the prologue. */
a1217d97 992 else
d53c26c7 993 break;
a1217d97
SL
994 }
995
d53c26c7 996 else if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
a1217d97 997 {
d53c26c7
SL
998 /* Branches not involving a stack overflow check aren't part of
999 the prologue. */
1000 if (ra != NIOS2_SP_REGNUM)
1001 break;
1002 else if (cond == branch_geu)
a1217d97 1003 {
d53c26c7 1004 /* BGEU sp, rx, +8
9aaf8e3a 1005 TRAP 3 (or BREAK 3)
d53c26c7
SL
1006 This instruction sequence is used in stack checking;
1007 we can ignore it. */
1008 unsigned int next_insn;
1009 const struct nios2_opcode *next_op
1010 = nios2_fetch_insn (gdbarch, pc, &next_insn);
1011 if (next_op != NULL
9aaf8e3a
SL
1012 && (nios2_match_trap (next_insn, op, mach, &uimm)
1013 || nios2_match_break (next_insn, op, mach, &uimm)))
d53c26c7
SL
1014 pc += next_op->size;
1015 else
1016 break;
a1217d97 1017 }
d53c26c7
SL
1018 else if (cond == branch_ltu)
1019 {
1020 /* BLTU sp, rx, .Lstackoverflow
9aaf8e3a
SL
1021 If the location branched to holds a TRAP or BREAK
1022 instruction then this is also stack overflow detection. */
d53c26c7
SL
1023 unsigned int next_insn;
1024 const struct nios2_opcode *next_op
1025 = nios2_fetch_insn (gdbarch, pc + imm, &next_insn);
1026 if (next_op != NULL
9aaf8e3a
SL
1027 && (nios2_match_trap (next_insn, op, mach, &uimm)
1028 || nios2_match_break (next_insn, op, mach, &uimm)))
d53c26c7
SL
1029 ;
1030 else
1031 break;
1032 }
1033 else
1034 break;
a1217d97
SL
1035 }
1036
9aaf8e3a 1037 /* All other calls, jumps, returns, TRAPs, or BREAKs terminate
d53c26c7
SL
1038 the prologue. */
1039 else if (nios2_match_callr (insn, op, mach, &ra)
1040 || nios2_match_jmpr (insn, op, mach, &ra)
9aaf8e3a
SL
1041 || nios2_match_jmpi (insn, op, mach, &uimm)
1042 || nios2_match_trap (insn, op, mach, &uimm)
1043 || nios2_match_break (insn, op, mach, &uimm))
d53c26c7 1044 break;
a1217d97
SL
1045 }
1046
1047 /* If THIS_FRAME is NULL, we are being called from skip_prologue
1048 and are only interested in the PROLOGUE_END value, so just
1049 return that now and skip over the cache updates, which depend
1050 on having frame information. */
1051 if (this_frame == NULL)
1052 return prologue_end;
1053
1054 /* If we are in the function epilogue and have already popped
1055 registers off the stack in preparation for returning, then we
1056 want to go back to the original register values. */
1057 if (innermost && nios2_in_epilogue_p (gdbarch, current_pc, start_pc))
1058 nios2_setup_default (cache);
1059
1060 /* Exception handlers use a different return address register. */
1061 if (exception_handler)
1062 cache->return_regnum = NIOS2_EA_REGNUM;
1063
1064 if (nios2_debug)
1065 fprintf_unfiltered (gdb_stdlog, "\n-> retreg=%d, ", cache->return_regnum);
1066
1067 if (cache->reg_value[NIOS2_FP_REGNUM].reg == NIOS2_SP_REGNUM)
1068 /* If the FP now holds an offset from the CFA then this is a
1069 normal frame which uses the frame pointer. */
1070 base_reg = NIOS2_FP_REGNUM;
1071 else if (cache->reg_value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM)
1072 /* FP doesn't hold an offset from the CFA. If SP still holds an
1073 offset from the CFA then we might be in a function which omits
1074 the frame pointer, or we might be partway through the prologue.
1075 In both cases we can find the CFA using SP. */
1076 base_reg = NIOS2_SP_REGNUM;
1077 else
1078 {
1079 /* Somehow the stack pointer has been corrupted.
1080 We can't return. */
1081 if (nios2_debug)
1082 fprintf_unfiltered (gdb_stdlog, "<can't reach cfa> }\n");
1083 return 0;
1084 }
1085
1086 if (cache->reg_value[base_reg].offset == 0
1087 || cache->reg_saved[NIOS2_RA_REGNUM].basereg != NIOS2_SP_REGNUM
1088 || cache->reg_saved[cache->return_regnum].basereg != NIOS2_SP_REGNUM)
1089 {
1090 /* If the frame didn't adjust the stack, didn't save RA or
1091 didn't save EA in an exception handler then it must either
1092 be a leaf function (doesn't call any other functions) or it
1093 can't return. If it has called another function then it
1094 can't be a leaf, so set base == 0 to indicate that we can't
1095 backtrace past it. */
1096
1097 if (!innermost)
1098 {
1099 /* If it isn't the innermost function then it can't be a
1100 leaf, unless it was interrupted. Check whether RA for
1101 this frame is the same as PC. If so then it probably
1102 wasn't interrupted. */
1103 CORE_ADDR ra
1104 = get_frame_register_unsigned (this_frame, NIOS2_RA_REGNUM);
1105
1106 if (ra == current_pc)
1107 {
1108 if (nios2_debug)
1109 fprintf_unfiltered
1110 (gdb_stdlog,
1111 "<noreturn ADJUST %s, r31@r%d+?>, r%d@r%d+?> }\n",
1112 paddress (gdbarch, cache->reg_value[base_reg].offset),
1113 cache->reg_saved[NIOS2_RA_REGNUM].basereg,
1114 cache->return_regnum,
1115 cache->reg_saved[cache->return_regnum].basereg);
1116 return 0;
1117 }
1118 }
1119 }
1120
1121 /* Get the value of whichever register we are using for the
1122 base. */
1123 cache->base = get_frame_register_unsigned (this_frame, base_reg);
1124
1125 /* What was the value of SP at the start of this function (or just
1126 after the stack switch). */
1127 frame_high = cache->base - cache->reg_value[base_reg].offset;
1128
1129 /* Adjust all the saved registers such that they contain addresses
1130 instead of offsets. */
1131 for (i = 0; i < NIOS2_NUM_REGS; i++)
1132 if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
1133 {
1134 cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
1135 cache->reg_saved[i].addr += frame_high;
1136 }
1137
1138 for (i = 0; i < NIOS2_NUM_REGS; i++)
1139 if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
1140 {
1141 CORE_ADDR gp = get_frame_register_unsigned (this_frame,
1142 NIOS2_GP_REGNUM);
1143
1144 for ( ; i < NIOS2_NUM_REGS; i++)
1145 if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
1146 {
1147 cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
1148 cache->reg_saved[i].addr += gp;
1149 }
1150 }
1151
1152 /* Work out what the value of SP was on the first instruction of
1153 this function. If we didn't switch stacks then this can be
1154 trivially computed from the base address. */
1155 if (cache->reg_saved[NIOS2_SP_REGNUM].basereg == NIOS2_Z_REGNUM)
1156 cache->cfa
1157 = read_memory_unsigned_integer (cache->reg_saved[NIOS2_SP_REGNUM].addr,
1158 4, byte_order);
1159 else
1160 cache->cfa = frame_high;
1161
1162 /* Exception handlers restore ESTATUS into STATUS. */
1163 if (exception_handler)
1164 {
1165 cache->reg_saved[NIOS2_STATUS_REGNUM]
1166 = cache->reg_saved[NIOS2_ESTATUS_REGNUM];
1167 cache->reg_saved[NIOS2_ESTATUS_REGNUM].basereg = -1;
1168 }
1169
1170 if (nios2_debug)
1171 fprintf_unfiltered (gdb_stdlog, "cfa=%s }\n",
1172 paddress (gdbarch, cache->cfa));
1173
1174 return prologue_end;
1175}
1176
1177/* Implement the skip_prologue gdbarch hook. */
1178
1179static CORE_ADDR
1180nios2_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1181{
a1217d97
SL
1182 CORE_ADDR func_addr;
1183
1184 struct nios2_unwind_cache cache;
1185
1186 /* See if we can determine the end of the prologue via the symbol
1187 table. If so, then return either PC, or the PC after the
1188 prologue, whichever is greater. */
1189 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
1190 {
1191 CORE_ADDR post_prologue_pc
1192 = skip_prologue_using_sal (gdbarch, func_addr);
1193
1194 if (post_prologue_pc != 0)
1195 return max (start_pc, post_prologue_pc);
1196 }
1197
1198 /* Prologue analysis does the rest.... */
1199 nios2_init_cache (&cache, start_pc);
1200 return nios2_analyze_prologue (gdbarch, start_pc, start_pc, &cache, NULL);
1201}
1202
b73c49b7
SL
1203/* Implement the breakpoint_from_pc gdbarch hook.
1204
1205 The Nios II ABI for Linux says: "Userspace programs should not use
1206 the break instruction and userspace debuggers should not insert
1207 one." and "Userspace breakpoints are accomplished using the trap
1208 instruction with immediate operand 31 (all ones)."
1209
1210 So, we use "trap 31" consistently as the breakpoint on bare-metal
1211 as well as Linux targets. */
a1217d97
SL
1212
1213static const gdb_byte*
1214nios2_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *bp_addr,
1215 int *bp_size)
1216{
a1217d97 1217 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
d53c26c7
SL
1218 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1219
b73c49b7
SL
1220 /* R1 trap encoding:
1221 ((0x1d << 17) | (0x2d << 11) | (0x1f << 6) | (0x3a << 0))
1222 0x003b6ffa */
1223 static const gdb_byte r1_breakpoint_le[] = {0xfa, 0x6f, 0x3b, 0x0};
1224 static const gdb_byte r1_breakpoint_be[] = {0x0, 0x3b, 0x6f, 0xfa};
d53c26c7
SL
1225 *bp_size = NIOS2_OPCODE_SIZE;
1226 if (byte_order_for_code == BFD_ENDIAN_BIG)
1227 return r1_breakpoint_be;
a1217d97 1228 else
d53c26c7 1229 return r1_breakpoint_le;
a1217d97
SL
1230}
1231
1232/* Implement the print_insn gdbarch method. */
1233
1234static int
1235nios2_print_insn (bfd_vma memaddr, disassemble_info *info)
1236{
1237 if (info->endian == BFD_ENDIAN_BIG)
1238 return print_insn_big_nios2 (memaddr, info);
1239 else
1240 return print_insn_little_nios2 (memaddr, info);
1241}
1242
1243
1244/* Implement the frame_align gdbarch method. */
1245
1246static CORE_ADDR
1247nios2_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1248{
1249 return align_down (addr, 4);
1250}
1251
1252
1253/* Implement the return_value gdbarch method. */
1254
1255static enum return_value_convention
1256nios2_return_value (struct gdbarch *gdbarch, struct value *function,
1257 struct type *type, struct regcache *regcache,
1258 gdb_byte *readbuf, const gdb_byte *writebuf)
1259{
1260 if (TYPE_LENGTH (type) > 8)
1261 return RETURN_VALUE_STRUCT_CONVENTION;
1262
1263 if (readbuf)
1264 nios2_extract_return_value (gdbarch, type, regcache, readbuf);
1265 if (writebuf)
1266 nios2_store_return_value (gdbarch, type, regcache, writebuf);
1267
1268 return RETURN_VALUE_REGISTER_CONVENTION;
1269}
1270
1271/* Implement the dummy_id gdbarch method. */
1272
1273static struct frame_id
1274nios2_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1275{
1276 return frame_id_build
1277 (get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM),
1278 get_frame_pc (this_frame));
1279}
1280
1281/* Implement the push_dummy_call gdbarch method. */
1282
1283static CORE_ADDR
1284nios2_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1285 struct regcache *regcache, CORE_ADDR bp_addr,
1286 int nargs, struct value **args, CORE_ADDR sp,
1287 int struct_return, CORE_ADDR struct_addr)
1288{
1289 int argreg;
1290 int float_argreg;
1291 int argnum;
1292 int len = 0;
1293 int stack_offset = 0;
1294 CORE_ADDR func_addr = find_function_addr (function, NULL);
1295 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1296
1297 /* Set the return address register to point to the entry point of
1298 the program, where a breakpoint lies in wait. */
1299 regcache_cooked_write_signed (regcache, NIOS2_RA_REGNUM, bp_addr);
1300
1301 /* Now make space on the stack for the args. */
1302 for (argnum = 0; argnum < nargs; argnum++)
1303 len += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);
1304 sp -= len;
1305
1306 /* Initialize the register pointer. */
1307 argreg = NIOS2_FIRST_ARGREG;
1308
1309 /* The struct_return pointer occupies the first parameter-passing
1310 register. */
1311 if (struct_return)
1312 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
1313
1314 /* Now load as many as possible of the first arguments into
1315 registers, and push the rest onto the stack. Loop through args
1316 from first to last. */
1317 for (argnum = 0; argnum < nargs; argnum++)
1318 {
1319 const gdb_byte *val;
1320 gdb_byte valbuf[MAX_REGISTER_SIZE];
1321 struct value *arg = args[argnum];
1322 struct type *arg_type = check_typedef (value_type (arg));
1323 int len = TYPE_LENGTH (arg_type);
1324 enum type_code typecode = TYPE_CODE (arg_type);
1325
1326 val = value_contents (arg);
1327
1328 /* Copy the argument to general registers or the stack in
1329 register-sized pieces. Large arguments are split between
1330 registers and stack. */
1331 while (len > 0)
1332 {
1333 int partial_len = (len < 4 ? len : 4);
1334
1335 if (argreg <= NIOS2_LAST_ARGREG)
1336 {
1337 /* The argument is being passed in a register. */
1338 CORE_ADDR regval = extract_unsigned_integer (val, partial_len,
1339 byte_order);
1340
1341 regcache_cooked_write_unsigned (regcache, argreg, regval);
1342 argreg++;
1343 }
1344 else
1345 {
1346 /* The argument is being passed on the stack. */
1347 CORE_ADDR addr = sp + stack_offset;
1348
1349 write_memory (addr, val, partial_len);
1350 stack_offset += align_up (partial_len, 4);
1351 }
1352
1353 len -= partial_len;
1354 val += partial_len;
1355 }
1356 }
1357
1358 regcache_cooked_write_signed (regcache, NIOS2_SP_REGNUM, sp);
1359
1360 /* Return adjusted stack pointer. */
1361 return sp;
1362}
1363
1364/* Implement the unwind_pc gdbarch method. */
1365
1366static CORE_ADDR
1367nios2_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1368{
1369 gdb_byte buf[4];
1370
1371 frame_unwind_register (next_frame, NIOS2_PC_REGNUM, buf);
1372 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1373}
1374
1375/* Implement the unwind_sp gdbarch method. */
1376
1377static CORE_ADDR
1378nios2_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
1379{
1380 return frame_unwind_register_unsigned (this_frame, NIOS2_SP_REGNUM);
1381}
1382
1383/* Use prologue analysis to fill in the register cache
1384 *THIS_PROLOGUE_CACHE for THIS_FRAME. This function initializes
1385 *THIS_PROLOGUE_CACHE first. */
1386
1387static struct nios2_unwind_cache *
1388nios2_frame_unwind_cache (struct frame_info *this_frame,
1389 void **this_prologue_cache)
1390{
1391 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1392 CORE_ADDR current_pc;
1393 struct nios2_unwind_cache *cache;
1394 int i;
1395
1396 if (*this_prologue_cache)
1397 return *this_prologue_cache;
1398
1399 cache = FRAME_OBSTACK_ZALLOC (struct nios2_unwind_cache);
1400 *this_prologue_cache = cache;
1401
1402 /* Zero all fields. */
1403 nios2_init_cache (cache, get_frame_func (this_frame));
1404
1405 /* Prologue analysis does the rest... */
1406 current_pc = get_frame_pc (this_frame);
1407 if (cache->pc != 0)
1408 nios2_analyze_prologue (gdbarch, cache->pc, current_pc, cache, this_frame);
1409
1410 return cache;
1411}
1412
1413/* Implement the this_id function for the normal unwinder. */
1414
1415static void
1416nios2_frame_this_id (struct frame_info *this_frame, void **this_cache,
1417 struct frame_id *this_id)
1418{
1419 struct nios2_unwind_cache *cache =
1420 nios2_frame_unwind_cache (this_frame, this_cache);
1421
1422 /* This marks the outermost frame. */
1423 if (cache->base == 0)
1424 return;
1425
1426 *this_id = frame_id_build (cache->cfa, cache->pc);
1427}
1428
1429/* Implement the prev_register function for the normal unwinder. */
1430
1431static struct value *
1432nios2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1433 int regnum)
1434{
1435 struct nios2_unwind_cache *cache =
1436 nios2_frame_unwind_cache (this_frame, this_cache);
1437
1438 gdb_assert (regnum >= 0 && regnum < NIOS2_NUM_REGS);
1439
1440 /* The PC of the previous frame is stored in the RA register of
1441 the current frame. Frob regnum so that we pull the value from
1442 the correct place. */
1443 if (regnum == NIOS2_PC_REGNUM)
1444 regnum = cache->return_regnum;
1445
1446 if (regnum == NIOS2_SP_REGNUM && cache->cfa)
1447 return frame_unwind_got_constant (this_frame, regnum, cache->cfa);
1448
1449 /* If we've worked out where a register is stored then load it from
1450 there. */
1451 if (cache->reg_saved[regnum].basereg == NIOS2_Z_REGNUM)
1452 return frame_unwind_got_memory (this_frame, regnum,
1453 cache->reg_saved[regnum].addr);
1454
1455 return frame_unwind_got_register (this_frame, regnum, regnum);
1456}
1457
1458/* Implement the this_base, this_locals, and this_args hooks
1459 for the normal unwinder. */
1460
1461static CORE_ADDR
1462nios2_frame_base_address (struct frame_info *this_frame, void **this_cache)
1463{
1464 struct nios2_unwind_cache *info
1465 = nios2_frame_unwind_cache (this_frame, this_cache);
1466
1467 return info->base;
1468}
1469
1470/* Data structures for the normal prologue-analysis-based
1471 unwinder. */
1472
1473static const struct frame_unwind nios2_frame_unwind =
1474{
1475 NORMAL_FRAME,
1476 default_frame_unwind_stop_reason,
1477 nios2_frame_this_id,
1478 nios2_frame_prev_register,
1479 NULL,
1480 default_frame_sniffer
1481};
1482
1483static const struct frame_base nios2_frame_base =
1484{
1485 &nios2_frame_unwind,
1486 nios2_frame_base_address,
1487 nios2_frame_base_address,
1488 nios2_frame_base_address
1489};
1490
1491/* Fill in the register cache *THIS_CACHE for THIS_FRAME for use
1492 in the stub unwinder. */
1493
1494static struct trad_frame_cache *
1495nios2_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
1496{
1497 CORE_ADDR pc;
1498 CORE_ADDR start_addr;
1499 CORE_ADDR stack_addr;
1500 struct trad_frame_cache *this_trad_cache;
1501 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1502 int num_regs = gdbarch_num_regs (gdbarch);
1503
1504 if (*this_cache != NULL)
1505 return *this_cache;
1506 this_trad_cache = trad_frame_cache_zalloc (this_frame);
1507 *this_cache = this_trad_cache;
1508
1509 /* The return address is in the link register. */
1510 trad_frame_set_reg_realreg (this_trad_cache,
1511 gdbarch_pc_regnum (gdbarch),
1512 NIOS2_RA_REGNUM);
1513
1514 /* Frame ID, since it's a frameless / stackless function, no stack
1515 space is allocated and SP on entry is the current SP. */
1516 pc = get_frame_pc (this_frame);
1517 find_pc_partial_function (pc, NULL, &start_addr, NULL);
1518 stack_addr = get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM);
1519 trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr));
1520 /* Assume that the frame's base is the same as the stack pointer. */
1521 trad_frame_set_this_base (this_trad_cache, stack_addr);
1522
1523 return this_trad_cache;
1524}
1525
1526/* Implement the this_id function for the stub unwinder. */
1527
1528static void
1529nios2_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
1530 struct frame_id *this_id)
1531{
1532 struct trad_frame_cache *this_trad_cache
1533 = nios2_stub_frame_cache (this_frame, this_cache);
1534
1535 trad_frame_get_id (this_trad_cache, this_id);
1536}
1537
1538/* Implement the prev_register function for the stub unwinder. */
1539
1540static struct value *
1541nios2_stub_frame_prev_register (struct frame_info *this_frame,
1542 void **this_cache, int regnum)
1543{
1544 struct trad_frame_cache *this_trad_cache
1545 = nios2_stub_frame_cache (this_frame, this_cache);
1546
1547 return trad_frame_get_register (this_trad_cache, this_frame, regnum);
1548}
1549
1550/* Implement the sniffer function for the stub unwinder.
1551 This unwinder is used for cases where the normal
1552 prologue-analysis-based unwinder can't work,
1553 such as PLT stubs. */
1554
1555static int
1556nios2_stub_frame_sniffer (const struct frame_unwind *self,
1557 struct frame_info *this_frame, void **cache)
1558{
1559 gdb_byte dummy[4];
1560 struct obj_section *s;
1561 CORE_ADDR pc = get_frame_address_in_block (this_frame);
1562
1563 /* Use the stub unwinder for unreadable code. */
1564 if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
1565 return 1;
1566
3e5d3a5a 1567 if (in_plt_section (pc))
a1217d97
SL
1568 return 1;
1569
1570 return 0;
1571}
1572
a1217d97
SL
1573/* Define the data structures for the stub unwinder. */
1574
1575static const struct frame_unwind nios2_stub_frame_unwind =
1576{
1577 NORMAL_FRAME,
1578 default_frame_unwind_stop_reason,
1579 nios2_stub_frame_this_id,
1580 nios2_stub_frame_prev_register,
1581 NULL,
1582 nios2_stub_frame_sniffer
1583};
1584
a1217d97 1585
a1217d97
SL
1586
1587/* Determine where to set a single step breakpoint while considering
1588 branch prediction. */
1589
1590static CORE_ADDR
1591nios2_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
1592{
1593 struct gdbarch *gdbarch = get_frame_arch (frame);
1594 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
d53c26c7
SL
1595 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1596 unsigned int insn;
1597 const struct nios2_opcode *op = nios2_fetch_insn (gdbarch, pc, &insn);
a1217d97
SL
1598 int ra;
1599 int rb;
d53c26c7
SL
1600 int imm;
1601 unsigned int uimm;
1602 int wb, ret;
1603 enum branch_condition cond;
1604
1605 /* Do something stupid if we can't disassemble the insn at pc. */
1606 if (op == NULL)
1607 return pc + NIOS2_OPCODE_SIZE;
1608
1609 if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
a1217d97 1610 {
d53c26c7
SL
1611 int ras = get_frame_register_signed (frame, ra);
1612 int rbs = get_frame_register_signed (frame, rb);
1613 unsigned int rau = get_frame_register_unsigned (frame, ra);
1614 unsigned int rbu = get_frame_register_unsigned (frame, rb);
a1217d97 1615
d53c26c7
SL
1616 pc += op->size;
1617 switch (cond)
a1217d97 1618 {
d53c26c7
SL
1619 case branch_none:
1620 pc += imm;
1621 break;
1622 case branch_eq:
1623 if (ras == rbs)
1624 pc += imm;
1625 break;
1626 case branch_ne:
1627 if (ras != rbs)
1628 pc += imm;
1629 break;
1630 case branch_ge:
1631 if (ras >= rbs)
1632 pc += imm;
1633 break;
1634 case branch_geu:
1635 if (rau >= rbu)
1636 pc += imm;
1637 break;
1638 case branch_lt:
1639 if (ras < rbs)
1640 pc += imm;
1641 break;
1642 case branch_ltu:
1643 if (rau < rbu)
1644 pc += imm;
a1217d97 1645 break;
a1217d97
SL
1646 default:
1647 break;
1648 }
a1217d97 1649 }
d53c26c7
SL
1650
1651 else if (nios2_match_jmpi (insn, op, mach, &uimm)
1652 || nios2_match_calli (insn, op, mach, &uimm))
1653 pc = (pc & 0xf0000000) | uimm;
1654
1655 else if (nios2_match_jmpr (insn, op, mach, &ra)
1656 || nios2_match_callr (insn, op, mach, &ra))
1657 pc = get_frame_register_unsigned (frame, ra);
1658
1659 else if (nios2_match_trap (insn, op, mach, &uimm))
1660 {
1661 if (tdep->syscall_next_pc != NULL)
1662 return tdep->syscall_next_pc (frame);
1663 }
1664
1665 else
1666 pc += op->size;
1667
a1217d97
SL
1668 return pc;
1669}
1670
1671/* Implement the software_single_step gdbarch method. */
1672
1673static int
1674nios2_software_single_step (struct frame_info *frame)
1675{
1676 struct gdbarch *gdbarch = get_frame_arch (frame);
1677 struct address_space *aspace = get_frame_address_space (frame);
1678 CORE_ADDR next_pc = nios2_get_next_pc (frame, get_frame_pc (frame));
1679
1680 insert_single_step_breakpoint (gdbarch, aspace, next_pc);
1681
1682 return 1;
1683}
1684
1685/* Implement the get_longjump_target gdbarch method. */
1686
1687static int
1688nios2_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1689{
1690 struct gdbarch *gdbarch = get_frame_arch (frame);
1691 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1692 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1693 CORE_ADDR jb_addr = get_frame_register_unsigned (frame, NIOS2_R4_REGNUM);
1694 gdb_byte buf[4];
1695
1696 if (target_read_memory (jb_addr + (tdep->jb_pc * 4), buf, 4))
1697 return 0;
1698
1699 *pc = extract_unsigned_integer (buf, 4, byte_order);
1700 return 1;
1701}
1702
1703/* Initialize the Nios II gdbarch. */
1704
1705static struct gdbarch *
1706nios2_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1707{
1708 struct gdbarch *gdbarch;
1709 struct gdbarch_tdep *tdep;
1710 int register_bytes, i;
1711 struct tdesc_arch_data *tdesc_data = NULL;
1712 const struct target_desc *tdesc = info.target_desc;
1713
1714 if (!tdesc_has_registers (tdesc))
1715 /* Pick a default target description. */
1716 tdesc = tdesc_nios2;
1717
1718 /* Check any target description for validity. */
1719 if (tdesc_has_registers (tdesc))
1720 {
1721 const struct tdesc_feature *feature;
1722 int valid_p;
1723
1724 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nios2.cpu");
1725 if (feature == NULL)
1726 return NULL;
1727
1728 tdesc_data = tdesc_data_alloc ();
1729
1730 valid_p = 1;
1731
1732 for (i = 0; i < NIOS2_NUM_REGS; i++)
1733 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
1734 nios2_reg_names[i]);
1735
1736 if (!valid_p)
1737 {
1738 tdesc_data_cleanup (tdesc_data);
1739 return NULL;
1740 }
1741 }
1742
1743 /* Find a candidate among the list of pre-declared architectures. */
1744 arches = gdbarch_list_lookup_by_info (arches, &info);
1745 if (arches != NULL)
1746 return arches->gdbarch;
1747
1748 /* None found, create a new architecture from the information
1749 provided. */
1750 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
1751 gdbarch = gdbarch_alloc (&info, tdep);
1752
1753 /* longjmp support not enabled by default. */
1754 tdep->jb_pc = -1;
1755
1756 /* Data type sizes. */
1757 set_gdbarch_ptr_bit (gdbarch, 32);
1758 set_gdbarch_addr_bit (gdbarch, 32);
1759 set_gdbarch_short_bit (gdbarch, 16);
1760 set_gdbarch_int_bit (gdbarch, 32);
1761 set_gdbarch_long_bit (gdbarch, 32);
1762 set_gdbarch_long_long_bit (gdbarch, 64);
1763 set_gdbarch_float_bit (gdbarch, 32);
1764 set_gdbarch_double_bit (gdbarch, 64);
1765
1766 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
1767 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
1768
1769 /* The register set. */
1770 set_gdbarch_num_regs (gdbarch, NIOS2_NUM_REGS);
1771 set_gdbarch_sp_regnum (gdbarch, NIOS2_SP_REGNUM);
1772 set_gdbarch_pc_regnum (gdbarch, NIOS2_PC_REGNUM); /* Pseudo register PC */
1773
1774 set_gdbarch_register_name (gdbarch, nios2_register_name);
1775 set_gdbarch_register_type (gdbarch, nios2_register_type);
1776
1777 /* Provide register mappings for stabs and dwarf2. */
1778 set_gdbarch_stab_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
1779 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
1780
1781 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1782
1783 /* Call dummy code. */
1784 set_gdbarch_frame_align (gdbarch, nios2_frame_align);
1785
1786 set_gdbarch_return_value (gdbarch, nios2_return_value);
1787
1788 set_gdbarch_skip_prologue (gdbarch, nios2_skip_prologue);
c9cf6e20 1789 set_gdbarch_stack_frame_destroyed_p (gdbarch, nios2_stack_frame_destroyed_p);
a1217d97
SL
1790 set_gdbarch_breakpoint_from_pc (gdbarch, nios2_breakpoint_from_pc);
1791
1792 set_gdbarch_dummy_id (gdbarch, nios2_dummy_id);
1793 set_gdbarch_unwind_pc (gdbarch, nios2_unwind_pc);
1794 set_gdbarch_unwind_sp (gdbarch, nios2_unwind_sp);
1795
1796 /* The dwarf2 unwinder will normally produce the best results if
1797 the debug information is available, so register it first. */
1798 dwarf2_append_unwinders (gdbarch);
1799 frame_unwind_append_unwinder (gdbarch, &nios2_stub_frame_unwind);
1800 frame_unwind_append_unwinder (gdbarch, &nios2_frame_unwind);
1801
1802 /* Single stepping. */
1803 set_gdbarch_software_single_step (gdbarch, nios2_software_single_step);
1804
1805 /* Hook in ABI-specific overrides, if they have been registered. */
1806 gdbarch_init_osabi (info, gdbarch);
1807
1808 if (tdep->jb_pc >= 0)
1809 set_gdbarch_get_longjmp_target (gdbarch, nios2_get_longjmp_target);
1810
1811 frame_base_set_default (gdbarch, &nios2_frame_base);
1812
1813 set_gdbarch_print_insn (gdbarch, nios2_print_insn);
1814
1815 /* Enable inferior call support. */
1816 set_gdbarch_push_dummy_call (gdbarch, nios2_push_dummy_call);
1817
1818 if (tdesc_data)
1819 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
1820
1821 return gdbarch;
1822}
1823
1824extern initialize_file_ftype _initialize_nios2_tdep; /* -Wmissing-prototypes */
1825
1826void
1827_initialize_nios2_tdep (void)
1828{
1829 gdbarch_register (bfd_arch_nios2, nios2_gdbarch_init, NULL);
1830 initialize_tdesc_nios2 ();
1831
1832 /* Allow debugging this file's internals. */
1833 add_setshow_boolean_cmd ("nios2", class_maintenance, &nios2_debug,
1834 _("Set Nios II debugging."),
1835 _("Show Nios II debugging."),
1836 _("When on, Nios II specific debugging is enabled."),
1837 NULL,
1838 NULL,
1839 &setdebuglist, &showdebuglist);
1840}
This page took 0.349608 seconds and 4 git commands to generate.