1 /* Target-machine dependent code for Nios II, for GDB.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 Contributed by Peter Brookes (pbrookes@altera.com)
4 and Andrew Draper (adraper@altera.com).
5 Contributed by Mentor Graphics, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
24 #include "frame-unwind.h"
25 #include "frame-base.h"
26 #include "trad-frame.h"
27 #include "dwarf2-frame.h"
39 #include "arch-utils.h"
40 #include "floatformat.h"
43 #include "target-descriptions.h"
45 /* To get entry_point_address. */
49 /* Nios II specific header. */
50 #include "nios2-tdep.h"
52 #include "features/nios2.c"
54 /* Control debugging information emitted in this file. */
56 static int nios2_debug = 0;
58 /* The following structures are used in the cache for prologue
59 analysis; see the reg_value and reg_saved tables in
60 struct nios2_unwind_cache, respectively. */
62 /* struct reg_value is used to record that a register has the same value
63 as reg at the given offset from the start of a function. */
71 /* struct reg_saved is used to record that a register value has been saved at
72 basereg + addr, for basereg >= 0. If basereg < 0, that indicates
73 that the register is not known to have been saved. Note that when
74 basereg == NIOS2_Z_REGNUM (that is, r0, which holds value 0),
75 addr is an absolute address. */
83 struct nios2_unwind_cache
85 /* The frame's base, optionally used by the high-level debug info. */
88 /* The previous frame's inner most stack address. Used as this
89 frame ID's stack_addr. */
92 /* The address of the first instruction in this function. */
95 /* Which register holds the return address for the frame. */
98 /* Table indicating what changes have been made to each register. */
99 struct reg_value reg_value[NIOS2_NUM_REGS];
101 /* Table indicating where each register has been saved. */
102 struct reg_saved reg_saved[NIOS2_NUM_REGS];
106 /* This array is a mapping from Dwarf-2 register numbering to GDB's. */
108 static int nios2_dwarf2gdb_regno_map[] =
117 NIOS2_GP_REGNUM, /* 26 */
118 NIOS2_SP_REGNUM, /* 27 */
119 NIOS2_FP_REGNUM, /* 28 */
120 NIOS2_EA_REGNUM, /* 29 */
121 NIOS2_BA_REGNUM, /* 30 */
122 NIOS2_RA_REGNUM, /* 31 */
123 NIOS2_PC_REGNUM, /* 32 */
124 NIOS2_STATUS_REGNUM, /* 33 */
125 NIOS2_ESTATUS_REGNUM, /* 34 */
126 NIOS2_BSTATUS_REGNUM, /* 35 */
127 NIOS2_IENABLE_REGNUM, /* 36 */
128 NIOS2_IPENDING_REGNUM, /* 37 */
129 NIOS2_CPUID_REGNUM, /* 38 */
130 39, /* CTL6 */ /* 39 */
131 NIOS2_EXCEPTION_REGNUM, /* 40 */
132 NIOS2_PTEADDR_REGNUM, /* 41 */
133 NIOS2_TLBACC_REGNUM, /* 42 */
134 NIOS2_TLBMISC_REGNUM, /* 43 */
135 NIOS2_ECCINJ_REGNUM, /* 44 */
136 NIOS2_BADADDR_REGNUM, /* 45 */
137 NIOS2_CONFIG_REGNUM, /* 46 */
138 NIOS2_MPUBASE_REGNUM, /* 47 */
139 NIOS2_MPUACC_REGNUM /* 48 */
142 gdb_static_assert (ARRAY_SIZE (nios2_dwarf2gdb_regno_map) == NIOS2_NUM_REGS);
144 /* Implement the dwarf2_reg_to_regnum gdbarch method. */
147 nios2_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
149 if (dw_reg < 0 || dw_reg >= NIOS2_NUM_REGS)
152 return nios2_dwarf2gdb_regno_map[dw_reg];
155 /* Canonical names for the 49 registers. */
157 static const char *const nios2_reg_names[NIOS2_NUM_REGS] =
159 "zero", "at", "r2", "r3", "r4", "r5", "r6", "r7",
160 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
161 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
162 "et", "bt", "gp", "sp", "fp", "ea", "sstatus", "ra",
164 "status", "estatus", "bstatus", "ienable",
165 "ipending", "cpuid", "ctl6", "exception",
166 "pteaddr", "tlbacc", "tlbmisc", "eccinj",
167 "badaddr", "config", "mpubase", "mpuacc"
170 /* Implement the register_name gdbarch method. */
173 nios2_register_name (struct gdbarch *gdbarch, int regno)
175 /* Use mnemonic aliases for GPRs. */
176 if (regno >= 0 && regno < NIOS2_NUM_REGS)
177 return nios2_reg_names[regno];
179 return tdesc_register_name (gdbarch, regno);
182 /* Implement the register_type gdbarch method. */
185 nios2_register_type (struct gdbarch *gdbarch, int regno)
187 /* If the XML description has register information, use that to
188 determine the register type. */
189 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
190 return tdesc_register_type (gdbarch, regno);
192 if (regno == NIOS2_PC_REGNUM)
193 return builtin_type (gdbarch)->builtin_func_ptr;
194 else if (regno == NIOS2_SP_REGNUM)
195 return builtin_type (gdbarch)->builtin_data_ptr;
197 return builtin_type (gdbarch)->builtin_uint32;
200 /* Given a return value in REGCACHE with a type VALTYPE,
201 extract and copy its value into VALBUF. */
204 nios2_extract_return_value (struct gdbarch *gdbarch, struct type *valtype,
205 struct regcache *regcache, gdb_byte *valbuf)
207 int len = TYPE_LENGTH (valtype);
209 /* Return values of up to 8 bytes are returned in $r2 $r3. */
210 if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
211 regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
214 gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
215 + register_size (gdbarch, NIOS2_R3_REGNUM)));
216 regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
217 regcache_cooked_read (regcache, NIOS2_R3_REGNUM, valbuf + 4);
221 /* Write into appropriate registers a function return value
222 of type TYPE, given in virtual format. */
225 nios2_store_return_value (struct gdbarch *gdbarch, struct type *valtype,
226 struct regcache *regcache, const gdb_byte *valbuf)
228 int len = TYPE_LENGTH (valtype);
230 /* Return values of up to 8 bytes are returned in $r2 $r3. */
231 if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
232 regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
235 gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
236 + register_size (gdbarch, NIOS2_R3_REGNUM)));
237 regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
238 regcache_cooked_write (regcache, NIOS2_R3_REGNUM, valbuf + 4);
243 /* Set up the default values of the registers. */
246 nios2_setup_default (struct nios2_unwind_cache *cache)
250 for (i = 0; i < NIOS2_NUM_REGS; i++)
252 /* All registers start off holding their previous values. */
253 cache->reg_value[i].reg = i;
254 cache->reg_value[i].offset = 0;
256 /* All registers start off not saved. */
257 cache->reg_saved[i].basereg = -1;
258 cache->reg_saved[i].addr = 0;
262 /* Initialize the unwind cache. */
265 nios2_init_cache (struct nios2_unwind_cache *cache, CORE_ADDR pc)
270 cache->return_regnum = NIOS2_RA_REGNUM;
271 nios2_setup_default (cache);
274 /* Read and identify an instruction at PC. If INSNP is non-null,
275 store the instruction word into that location. Return the opcode
276 pointer or NULL if the memory couldn't be read or disassembled. */
278 static const struct nios2_opcode *
279 nios2_fetch_insn (struct gdbarch *gdbarch, CORE_ADDR pc,
283 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
286 if (mach == bfd_mach_nios2r2)
288 if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
289 BFD_ENDIAN_LITTLE, &memword)
290 && !safe_read_memory_integer (pc, NIOS2_CDX_OPCODE_SIZE,
291 BFD_ENDIAN_LITTLE, &memword))
294 else if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
295 gdbarch_byte_order (gdbarch), &memword))
298 insn = (unsigned int) memword;
301 return nios2_find_opcode_hash (insn, mach);
305 /* Match and disassemble an ADD-type instruction, with 3 register operands.
306 Returns true on success, and fills in the operand pointers. */
309 nios2_match_add (uint32_t insn, const struct nios2_opcode *op,
310 unsigned long mach, int *ra, int *rb, int *rc)
312 int is_r2 = (mach == bfd_mach_nios2r2);
314 if (!is_r2 && (op->match == MATCH_R1_ADD || op->match == MATCH_R1_MOV))
316 *ra = GET_IW_R_A (insn);
317 *rb = GET_IW_R_B (insn);
318 *rc = GET_IW_R_C (insn);
323 else if (op->match == MATCH_R2_ADD || op->match == MATCH_R2_MOV)
325 *ra = GET_IW_F3X6L5_A (insn);
326 *rb = GET_IW_F3X6L5_B (insn);
327 *rc = GET_IW_F3X6L5_C (insn);
330 else if (op->match == MATCH_R2_ADD_N)
332 *ra = nios2_r2_reg3_mappings[GET_IW_T3X1_A3 (insn)];
333 *rb = nios2_r2_reg3_mappings[GET_IW_T3X1_B3 (insn)];
334 *rc = nios2_r2_reg3_mappings[GET_IW_T3X1_C3 (insn)];
337 else if (op->match == MATCH_R2_MOV_N)
339 *ra = GET_IW_F2_A (insn);
341 *rc = GET_IW_F2_B (insn);
347 /* Match and disassemble a SUB-type instruction, with 3 register operands.
348 Returns true on success, and fills in the operand pointers. */
351 nios2_match_sub (uint32_t insn, const struct nios2_opcode *op,
352 unsigned long mach, int *ra, int *rb, int *rc)
354 int is_r2 = (mach == bfd_mach_nios2r2);
356 if (!is_r2 && op->match == MATCH_R1_SUB)
358 *ra = GET_IW_R_A (insn);
359 *rb = GET_IW_R_B (insn);
360 *rc = GET_IW_R_C (insn);
365 else if (op->match == MATCH_R2_SUB)
367 *ra = GET_IW_F3X6L5_A (insn);
368 *rb = GET_IW_F3X6L5_B (insn);
369 *rc = GET_IW_F3X6L5_C (insn);
372 else if (op->match == MATCH_R2_SUB_N)
374 *ra = nios2_r2_reg3_mappings[GET_IW_T3X1_A3 (insn)];
375 *rb = nios2_r2_reg3_mappings[GET_IW_T3X1_B3 (insn)];
376 *rc = nios2_r2_reg3_mappings[GET_IW_T3X1_C3 (insn)];
382 /* Match and disassemble an ADDI-type instruction, with 2 register operands
383 and one immediate operand.
384 Returns true on success, and fills in the operand pointers. */
387 nios2_match_addi (uint32_t insn, const struct nios2_opcode *op,
388 unsigned long mach, int *ra, int *rb, int *imm)
390 int is_r2 = (mach == bfd_mach_nios2r2);
392 if (!is_r2 && op->match == MATCH_R1_ADDI)
394 *ra = GET_IW_I_A (insn);
395 *rb = GET_IW_I_B (insn);
396 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
401 else if (op->match == MATCH_R2_ADDI)
403 *ra = GET_IW_F2I16_A (insn);
404 *rb = GET_IW_F2I16_B (insn);
405 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
408 else if (op->match == MATCH_R2_ADDI_N || op->match == MATCH_R2_SUBI_N)
410 *ra = nios2_r2_reg3_mappings[GET_IW_T2X1I3_A3 (insn)];
411 *rb = nios2_r2_reg3_mappings[GET_IW_T2X1I3_B3 (insn)];
412 *imm = nios2_r2_asi_n_mappings[GET_IW_T2X1I3_IMM3 (insn)];
413 if (op->match == MATCH_R2_SUBI_N)
417 else if (op->match == MATCH_R2_SPADDI_N)
419 *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
420 *rb = NIOS2_SP_REGNUM;
421 *imm = GET_IW_T1I7_IMM7 (insn) << 2;
424 else if (op->match == MATCH_R2_SPINCI_N || op->match == MATCH_R2_SPDECI_N)
426 *ra = NIOS2_SP_REGNUM;
427 *rb = NIOS2_SP_REGNUM;
428 *imm = GET_IW_X1I7_IMM7 (insn) << 2;
429 if (op->match == MATCH_R2_SPDECI_N)
436 /* Match and disassemble an ORHI-type instruction, with 2 register operands
437 and one unsigned immediate operand.
438 Returns true on success, and fills in the operand pointers. */
441 nios2_match_orhi (uint32_t insn, const struct nios2_opcode *op,
442 unsigned long mach, int *ra, int *rb, unsigned int *uimm)
444 int is_r2 = (mach == bfd_mach_nios2r2);
446 if (!is_r2 && op->match == MATCH_R1_ORHI)
448 *ra = GET_IW_I_A (insn);
449 *rb = GET_IW_I_B (insn);
450 *uimm = GET_IW_I_IMM16 (insn);
455 else if (op->match == MATCH_R2_ORHI)
457 *ra = GET_IW_F2I16_A (insn);
458 *rb = GET_IW_F2I16_B (insn);
459 *uimm = GET_IW_F2I16_IMM16 (insn);
465 /* Match and disassemble a STW-type instruction, with 2 register operands
466 and one immediate operand.
467 Returns true on success, and fills in the operand pointers. */
470 nios2_match_stw (uint32_t insn, const struct nios2_opcode *op,
471 unsigned long mach, int *ra, int *rb, int *imm)
473 int is_r2 = (mach == bfd_mach_nios2r2);
475 if (!is_r2 && (op->match == MATCH_R1_STW || op->match == MATCH_R1_STWIO))
477 *ra = GET_IW_I_A (insn);
478 *rb = GET_IW_I_B (insn);
479 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
484 else if (op->match == MATCH_R2_STW)
486 *ra = GET_IW_F2I16_A (insn);
487 *rb = GET_IW_F2I16_B (insn);
488 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
491 else if (op->match == MATCH_R2_STWIO)
493 *ra = GET_IW_F2X4I12_A (insn);
494 *rb = GET_IW_F2X4I12_B (insn);
495 *imm = (signed) (GET_IW_F2X4I12_IMM12 (insn) << 20) >> 20;
498 else if (op->match == MATCH_R2_STW_N)
500 *ra = nios2_r2_reg3_mappings[GET_IW_T2I4_A3 (insn)];
501 *rb = nios2_r2_reg3_mappings[GET_IW_T2I4_B3 (insn)];
502 *imm = GET_IW_T2I4_IMM4 (insn) << 2;
505 else if (op->match == MATCH_R2_STWSP_N)
507 *ra = NIOS2_SP_REGNUM;
508 *rb = GET_IW_F1I5_B (insn);
509 *imm = GET_IW_F1I5_IMM5 (insn) << 2;
512 else if (op->match == MATCH_R2_STWZ_N)
514 *ra = nios2_r2_reg3_mappings[GET_IW_T1X1I6_A3 (insn)];
516 *imm = GET_IW_T1X1I6_IMM6 (insn) << 2;
522 /* Match and disassemble a LDW-type instruction, with 2 register operands
523 and one immediate operand.
524 Returns true on success, and fills in the operand pointers. */
527 nios2_match_ldw (uint32_t insn, const struct nios2_opcode *op,
528 unsigned long mach, int *ra, int *rb, int *imm)
530 int is_r2 = (mach == bfd_mach_nios2r2);
532 if (!is_r2 && (op->match == MATCH_R1_LDW || op->match == MATCH_R1_LDWIO))
534 *ra = GET_IW_I_A (insn);
535 *rb = GET_IW_I_B (insn);
536 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
541 else if (op->match == MATCH_R2_LDW)
543 *ra = GET_IW_F2I16_A (insn);
544 *rb = GET_IW_F2I16_B (insn);
545 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
548 else if (op->match == MATCH_R2_LDWIO)
550 *ra = GET_IW_F2X4I12_A (insn);
551 *rb = GET_IW_F2X4I12_B (insn);
552 *imm = (signed) (GET_IW_F2X4I12_IMM12 (insn) << 20) >> 20;
555 else if (op->match == MATCH_R2_LDW_N)
557 *ra = nios2_r2_reg3_mappings[GET_IW_T2I4_A3 (insn)];
558 *rb = nios2_r2_reg3_mappings[GET_IW_T2I4_B3 (insn)];
559 *imm = GET_IW_T2I4_IMM4 (insn) << 2;
562 else if (op->match == MATCH_R2_LDWSP_N)
564 *ra = NIOS2_SP_REGNUM;
565 *rb = GET_IW_F1I5_B (insn);
566 *imm = GET_IW_F1I5_IMM5 (insn) << 2;
572 /* Match and disassemble a RDCTL instruction, with 2 register operands.
573 Returns true on success, and fills in the operand pointers. */
576 nios2_match_rdctl (uint32_t insn, const struct nios2_opcode *op,
577 unsigned long mach, int *ra, int *rc)
579 int is_r2 = (mach == bfd_mach_nios2r2);
581 if (!is_r2 && (op->match == MATCH_R1_RDCTL))
583 *ra = GET_IW_R_IMM5 (insn);
584 *rc = GET_IW_R_C (insn);
589 else if (op->match == MATCH_R2_RDCTL)
591 *ra = GET_IW_F3X6L5_IMM5 (insn);
592 *rc = GET_IW_F3X6L5_C (insn);
598 /* Match and disassemble a PUSH.N or STWM instruction.
599 Returns true on success, and fills in the operand pointers. */
602 nios2_match_stwm (uint32_t insn, const struct nios2_opcode *op,
603 unsigned long mach, unsigned int *reglist,
604 int *ra, int *imm, int *wb, int *id)
606 int is_r2 = (mach == bfd_mach_nios2r2);
610 else if (op->match == MATCH_R2_PUSH_N)
613 if (GET_IW_L5I4X1_FP (insn))
614 *reglist |= (1 << 28);
615 if (GET_IW_L5I4X1_CS (insn))
617 int val = GET_IW_L5I4X1_REGRANGE (insn);
618 *reglist |= nios2_r2_reg_range_mappings[val];
620 *ra = NIOS2_SP_REGNUM;
621 *imm = GET_IW_L5I4X1_IMM4 (insn) << 2;
626 else if (op->match == MATCH_R2_STWM)
628 unsigned int rawmask = GET_IW_F1X4L17_REGMASK (insn);
629 if (GET_IW_F1X4L17_RS (insn))
631 *reglist = ((rawmask << 14) & 0x00ffc000);
632 if (rawmask & (1 << 10))
633 *reglist |= (1 << 28);
634 if (rawmask & (1 << 11))
635 *reglist |= (1 << 31);
638 *reglist = rawmask << 2;
639 *ra = GET_IW_F1X4L17_A (insn);
641 *wb = GET_IW_F1X4L17_WB (insn);
642 *id = GET_IW_F1X4L17_ID (insn);
648 /* Match and disassemble a POP.N or LDWM instruction.
649 Returns true on success, and fills in the operand pointers. */
652 nios2_match_ldwm (uint32_t insn, const struct nios2_opcode *op,
653 unsigned long mach, unsigned int *reglist,
654 int *ra, int *imm, int *wb, int *id, int *ret)
656 int is_r2 = (mach == bfd_mach_nios2r2);
660 else if (op->match == MATCH_R2_POP_N)
663 if (GET_IW_L5I4X1_FP (insn))
664 *reglist |= (1 << 28);
665 if (GET_IW_L5I4X1_CS (insn))
667 int val = GET_IW_L5I4X1_REGRANGE (insn);
668 *reglist |= nios2_r2_reg_range_mappings[val];
670 *ra = NIOS2_SP_REGNUM;
671 *imm = GET_IW_L5I4X1_IMM4 (insn) << 2;
677 else if (op->match == MATCH_R2_LDWM)
679 unsigned int rawmask = GET_IW_F1X4L17_REGMASK (insn);
680 if (GET_IW_F1X4L17_RS (insn))
682 *reglist = ((rawmask << 14) & 0x00ffc000);
683 if (rawmask & (1 << 10))
684 *reglist |= (1 << 28);
685 if (rawmask & (1 << 11))
686 *reglist |= (1 << 31);
689 *reglist = rawmask << 2;
690 *ra = GET_IW_F1X4L17_A (insn);
692 *wb = GET_IW_F1X4L17_WB (insn);
693 *id = GET_IW_F1X4L17_ID (insn);
694 *ret = GET_IW_F1X4L17_PC (insn);
700 /* Match and disassemble a branch instruction, with (potentially)
701 2 register operands and one immediate operand.
702 Returns true on success, and fills in the operand pointers. */
704 enum branch_condition {
715 nios2_match_branch (uint32_t insn, const struct nios2_opcode *op,
716 unsigned long mach, int *ra, int *rb, int *imm,
717 enum branch_condition *cond)
719 int is_r2 = (mach == bfd_mach_nios2r2);
749 *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
750 *ra = GET_IW_I_A (insn);
751 *rb = GET_IW_I_B (insn);
760 *ra = NIOS2_Z_REGNUM;
761 *rb = NIOS2_Z_REGNUM;
762 *imm = (signed) ((GET_IW_I10_IMM10 (insn) << 1) << 21) >> 21;
764 case MATCH_R2_BEQZ_N:
766 *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
767 *rb = NIOS2_Z_REGNUM;
768 *imm = (signed) ((GET_IW_T1I7_IMM7 (insn) << 1) << 24) >> 24;
770 case MATCH_R2_BNEZ_N:
772 *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
773 *rb = NIOS2_Z_REGNUM;
774 *imm = (signed) ((GET_IW_T1I7_IMM7 (insn) << 1) << 24) >> 24;
800 *ra = GET_IW_F2I16_A (insn);
801 *rb = GET_IW_F2I16_B (insn);
802 *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
808 /* Match and disassemble a direct jump instruction, with an
809 unsigned operand. Returns true on success, and fills in the operand
813 nios2_match_jmpi (uint32_t insn, const struct nios2_opcode *op,
814 unsigned long mach, unsigned int *uimm)
816 int is_r2 = (mach == bfd_mach_nios2r2);
818 if (!is_r2 && op->match == MATCH_R1_JMPI)
820 *uimm = GET_IW_J_IMM26 (insn) << 2;
825 else if (op->match == MATCH_R2_JMPI)
827 *uimm = GET_IW_L26_IMM26 (insn) << 2;
833 /* Match and disassemble a direct call instruction, with an
834 unsigned operand. Returns true on success, and fills in the operand
838 nios2_match_calli (uint32_t insn, const struct nios2_opcode *op,
839 unsigned long mach, unsigned int *uimm)
841 int is_r2 = (mach == bfd_mach_nios2r2);
843 if (!is_r2 && op->match == MATCH_R1_CALL)
845 *uimm = GET_IW_J_IMM26 (insn) << 2;
850 else if (op->match == MATCH_R2_CALL)
852 *uimm = GET_IW_L26_IMM26 (insn) << 2;
858 /* Match and disassemble an indirect jump instruction, with a
859 (possibly implicit) register operand. Returns true on success, and fills
860 in the operand pointer. */
863 nios2_match_jmpr (uint32_t insn, const struct nios2_opcode *op,
864 unsigned long mach, int *ra)
866 int is_r2 = (mach == bfd_mach_nios2r2);
872 *ra = GET_IW_I_A (insn);
875 *ra = NIOS2_RA_REGNUM;
878 *ra = NIOS2_EA_REGNUM;
881 *ra = NIOS2_BA_REGNUM;
890 *ra = GET_IW_F2I16_A (insn);
892 case MATCH_R2_JMPR_N:
893 *ra = GET_IW_F1X1_A (insn);
897 *ra = NIOS2_RA_REGNUM;
900 *ra = NIOS2_EA_REGNUM;
903 *ra = NIOS2_BA_REGNUM;
911 /* Match and disassemble an indirect call instruction, with a register
912 operand. Returns true on success, and fills in the operand pointer. */
915 nios2_match_callr (uint32_t insn, const struct nios2_opcode *op,
916 unsigned long mach, int *ra)
918 int is_r2 = (mach == bfd_mach_nios2r2);
920 if (!is_r2 && op->match == MATCH_R1_CALLR)
922 *ra = GET_IW_I_A (insn);
927 else if (op->match == MATCH_R2_CALLR)
929 *ra = GET_IW_F2I16_A (insn);
932 else if (op->match == MATCH_R2_CALLR_N)
934 *ra = GET_IW_F1X1_A (insn);
940 /* Match and disassemble a break instruction, with an unsigned operand.
941 Returns true on success, and fills in the operand pointer. */
944 nios2_match_break (uint32_t insn, const struct nios2_opcode *op,
945 unsigned long mach, unsigned int *uimm)
947 int is_r2 = (mach == bfd_mach_nios2r2);
949 if (!is_r2 && op->match == MATCH_R1_BREAK)
951 *uimm = GET_IW_R_IMM5 (insn);
956 else if (op->match == MATCH_R2_BREAK)
958 *uimm = GET_IW_F3X6L5_IMM5 (insn);
961 else if (op->match == MATCH_R2_BREAK_N)
963 *uimm = GET_IW_X2L5_IMM5 (insn);
969 /* Match and disassemble a trap instruction, with an unsigned operand.
970 Returns true on success, and fills in the operand pointer. */
973 nios2_match_trap (uint32_t insn, const struct nios2_opcode *op,
974 unsigned long mach, unsigned int *uimm)
976 int is_r2 = (mach == bfd_mach_nios2r2);
978 if (!is_r2 && op->match == MATCH_R1_TRAP)
980 *uimm = GET_IW_R_IMM5 (insn);
985 else if (op->match == MATCH_R2_TRAP)
987 *uimm = GET_IW_F3X6L5_IMM5 (insn);
990 else if (op->match == MATCH_R2_TRAP_N)
992 *uimm = GET_IW_X2L5_IMM5 (insn);
998 /* Helper function to identify when we're in a function epilogue;
999 that is, the part of the function from the point at which the
1000 stack adjustments are made, to the return or sibcall.
1001 Note that we may have several stack adjustment instructions, and
1002 this function needs to test whether the stack teardown has already
1003 started before current_pc, not whether it has completed. */
1006 nios2_in_epilogue_p (struct gdbarch *gdbarch,
1007 CORE_ADDR current_pc,
1010 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1011 int is_r2 = (mach == bfd_mach_nios2r2);
1012 /* Maximum number of possibly-epilogue instructions to check.
1013 Note that this number should not be too large, else we can
1014 potentially end up iterating through unmapped memory. */
1015 int ninsns, max_insns = 5;
1017 const struct nios2_opcode *op = NULL;
1022 enum branch_condition cond;
1025 /* There has to be a previous instruction in the function. */
1026 if (current_pc <= start_pc)
1029 /* Find the previous instruction before current_pc. For R2, it might
1030 be either a 16-bit or 32-bit instruction; the only way to know for
1031 sure is to scan through from the beginning of the function,
1032 disassembling as we go. */
1034 for (pc = start_pc; ; )
1036 op = nios2_fetch_insn (gdbarch, pc, &insn);
1039 if (pc + op->size < current_pc)
1043 /* We can skip over insns to a forward branch target. Since
1044 the branch offset is relative to the next instruction,
1045 it's correct to do this after incrementing the pc above. */
1046 if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
1048 && pc + imm < current_pc)
1051 /* Otherwise just go back to the previous 32-bit insn. */
1053 pc = current_pc - NIOS2_OPCODE_SIZE;
1055 /* Beginning with the previous instruction we just located, check whether
1056 we are in a sequence of at least one stack adjustment instruction.
1057 Possible instructions here include:
1063 LDWM {reglist}, (sp)++, wb */
1064 for (ninsns = 0; ninsns < max_insns; ninsns++)
1068 /* Fetch the insn at pc. */
1069 op = nios2_fetch_insn (gdbarch, pc, &insn);
1074 /* Was it a stack adjustment? */
1075 if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
1076 ok = (rb == NIOS2_SP_REGNUM);
1077 else if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
1078 ok = (rc == NIOS2_SP_REGNUM);
1079 else if (nios2_match_ldw (insn, op, mach, &ra, &rb, &imm))
1080 ok = (rb == NIOS2_SP_REGNUM);
1081 else if (nios2_match_ldwm (insn, op, mach, &uimm, &ra,
1082 &imm, &wb, &ret, &id))
1083 ok = (ra == NIOS2_SP_REGNUM && wb && id);
1088 /* No stack adjustments found. */
1092 /* We found more stack adjustments than we expect GCC to be generating.
1093 Since it looks like a stack unwind might be in progress tell GDB to
1094 treat it as such. */
1095 if (ninsns == max_insns)
1098 /* The next instruction following the stack adjustments must be a
1099 return, jump, or unconditional branch, or a CDX pop.n or ldwm
1100 that does an implicit return. */
1101 if (nios2_match_jmpr (insn, op, mach, &ra)
1102 || nios2_match_jmpi (insn, op, mach, &uimm)
1103 || (nios2_match_ldwm (insn, op, mach, &uimm, &ra, &imm, &wb, &id, &ret)
1105 || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
1106 && cond == branch_none))
1112 /* Implement the stack_frame_destroyed_p gdbarch method. */
1115 nios2_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1117 CORE_ADDR func_addr;
1119 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1120 return nios2_in_epilogue_p (gdbarch, pc, func_addr);
1125 /* Do prologue analysis, returning the PC of the first instruction
1126 after the function prologue. Assumes CACHE has already been
1127 initialized. THIS_FRAME can be null, in which case we are only
1128 interested in skipping the prologue. Otherwise CACHE is filled in
1129 from the frame information.
1131 The prologue may consist of the following parts:
1132 1) Profiling instrumentation. For non-PIC code it looks like:
1137 2) A stack adjustment and save of R4-R7 for varargs functions.
1138 For R2 CDX this is typically handled with a STWM, otherwise
1139 this is typically merged with item 3.
1141 3) A stack adjustment and save of the callee-saved registers.
1142 For R2 CDX these are typically handled with a PUSH.N or STWM,
1143 otherwise as an explicit SP decrement and individual register
1146 There may also be a stack switch here in an exception handler
1147 in place of a stack adjustment. It looks like:
1148 movhi rx, %hiadj(newstack)
1149 addhi rx, rx, %lo(newstack)
1150 stw sp, constant(rx)
1153 4) A frame pointer save, which can be either a MOV or ADDI.
1155 5) A further stack pointer adjustment. This is normally included
1156 adjustment in step 3 unless the total adjustment is too large
1157 to be done in one step.
1159 7) A stack overflow check, which can take either of these forms:
1163 bltu sp, rx, .Lstack_overflow
1168 Older versions of GCC emitted "break 3" instead of "trap 3" here,
1169 so we check for both cases.
1171 Older GCC versions emitted stack overflow checks after the SP
1172 adjustments in both steps 3 and 4. Starting with GCC 6, there is
1173 at most one overflow check, which is placed before the first
1174 stack adjustment for R2 CDX and after the first stack adjustment
1177 The prologue instructions may be combined or interleaved with other
1180 To cope with all this variability we decode all the instructions
1181 from the start of the prologue until we hit an instruction that
1182 cannot possibly be a prologue instruction, such as a branch, call,
1183 return, or epilogue instruction. The prologue is considered to end
1184 at the last instruction that can definitely be considered a
1185 prologue instruction. */
1188 nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc,
1189 const CORE_ADDR current_pc,
1190 struct nios2_unwind_cache *cache,
1191 struct frame_info *this_frame)
1193 /* Maximum number of possibly-prologue instructions to check.
1194 Note that this number should not be too large, else we can
1195 potentially end up iterating through unmapped memory. */
1196 int ninsns, max_insns = 50;
1197 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1198 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1200 /* Does the frame set up the FP register? */
1203 struct reg_value *value = cache->reg_value;
1204 struct reg_value temp_value[NIOS2_NUM_REGS];
1208 /* Save the starting PC so we can correct the pc after running
1209 through the prolog, using symbol info. */
1210 CORE_ADDR pc = start_pc;
1212 /* Is this an exception handler? */
1213 int exception_handler = 0;
1215 /* What was the original value of SP (or fake original value for
1216 functions which switch stacks? */
1217 CORE_ADDR frame_high;
1219 /* The last definitely-prologue instruction seen. */
1220 CORE_ADDR prologue_end;
1222 /* Is this the innermost function? */
1223 int innermost = (this_frame ? (frame_relative_level (this_frame) == 0) : 1);
1226 fprintf_unfiltered (gdb_stdlog,
1227 "{ nios2_analyze_prologue start=%s, current=%s ",
1228 paddress (gdbarch, start_pc),
1229 paddress (gdbarch, current_pc));
1231 /* Set up the default values of the registers. */
1232 nios2_setup_default (cache);
1234 /* Find the prologue instructions. */
1235 prologue_end = start_pc;
1236 for (ninsns = 0; ninsns < max_insns; ninsns++)
1238 /* Present instruction. */
1240 const struct nios2_opcode *op;
1241 int ra, rb, rc, imm;
1243 unsigned int reglist;
1245 enum branch_condition cond;
1247 if (pc == current_pc)
1249 /* When we reach the current PC we must save the current
1250 register state (for the backtrace) but keep analysing
1251 because there might be more to find out (eg. is this an
1252 exception handler). */
1253 memcpy (temp_value, value, sizeof (temp_value));
1256 fprintf_unfiltered (gdb_stdlog, "*");
1259 op = nios2_fetch_insn (gdbarch, pc, &insn);
1261 /* Unknown opcode? Stop scanning. */
1269 fprintf_unfiltered (gdb_stdlog, "[%04X]", insn & 0xffff);
1271 fprintf_unfiltered (gdb_stdlog, "[%08X]", insn);
1274 /* The following instructions can appear in the prologue. */
1276 if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
1278 /* ADD rc, ra, rb (also used for MOV) */
1279 if (rc == NIOS2_SP_REGNUM
1281 && value[ra].reg == cache->reg_saved[NIOS2_SP_REGNUM].basereg)
1283 /* If the previous value of SP is available somewhere
1284 near the new stack pointer value then this is a
1287 /* If any registers were saved on the stack before then
1288 we can't backtrace into them now. */
1289 for (i = 0 ; i < NIOS2_NUM_REGS ; i++)
1291 if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
1292 cache->reg_saved[i].basereg = -1;
1293 if (value[i].reg == NIOS2_SP_REGNUM)
1297 /* Create a fake "high water mark" 4 bytes above where SP
1298 was stored and fake up the registers to be consistent
1300 value[NIOS2_SP_REGNUM].reg = NIOS2_SP_REGNUM;
1301 value[NIOS2_SP_REGNUM].offset
1303 - cache->reg_saved[NIOS2_SP_REGNUM].addr
1305 cache->reg_saved[NIOS2_SP_REGNUM].basereg = NIOS2_SP_REGNUM;
1306 cache->reg_saved[NIOS2_SP_REGNUM].addr = -4;
1309 else if (rc == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
1310 /* This is setting SP from FP. This only happens in the
1311 function epilogue. */
1316 if (value[rb].reg == 0)
1317 value[rc].reg = value[ra].reg;
1318 else if (value[ra].reg == 0)
1319 value[rc].reg = value[rb].reg;
1322 value[rc].offset = value[ra].offset + value[rb].offset;
1325 /* The add/move is only considered a prologue instruction
1326 if the destination is SP or FP. */
1327 if (rc == NIOS2_SP_REGNUM || rc == NIOS2_FP_REGNUM)
1331 else if (nios2_match_sub (insn, op, mach, &ra, &rb, &rc))
1333 /* SUB rc, ra, rb */
1334 if (rc == NIOS2_SP_REGNUM && rb == NIOS2_SP_REGNUM
1335 && value[rc].reg != 0)
1336 /* If we are decrementing the SP by a non-constant amount,
1337 this is alloca, not part of the prologue. */
1341 if (value[rb].reg == 0)
1342 value[rc].reg = value[ra].reg;
1345 value[rc].offset = value[ra].offset - value[rb].offset;
1349 else if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
1351 /* ADDI rb, ra, imm */
1353 /* A positive stack adjustment has to be part of the epilogue. */
1354 if (rb == NIOS2_SP_REGNUM
1355 && (imm > 0 || value[ra].reg != NIOS2_SP_REGNUM))
1358 /* Likewise restoring SP from FP. */
1359 else if (rb == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
1364 value[rb].reg = value[ra].reg;
1365 value[rb].offset = value[ra].offset + imm;
1368 /* The add is only considered a prologue instruction
1369 if the destination is SP or FP. */
1370 if (rb == NIOS2_SP_REGNUM || rb == NIOS2_FP_REGNUM)
1374 else if (nios2_match_orhi (insn, op, mach, &ra, &rb, &uimm))
1376 /* ORHI rb, ra, uimm (also used for MOVHI) */
1379 value[rb].reg = (value[ra].reg == 0) ? 0 : -1;
1380 value[rb].offset = value[ra].offset | (uimm << 16);
1384 else if (nios2_match_stw (insn, op, mach, &ra, &rb, &imm))
1386 /* STW rb, imm(ra) */
1388 /* Are we storing the original value of a register to the stack?
1389 For exception handlers the value of EA-4 (return
1390 address from interrupts etc) is sometimes stored. */
1391 int orig = value[rb].reg;
1393 && (value[rb].offset == 0
1394 || (orig == NIOS2_EA_REGNUM && value[rb].offset == -4))
1395 && value[ra].reg == NIOS2_SP_REGNUM)
1397 if (pc < current_pc)
1399 /* Save off callee saved registers. */
1400 cache->reg_saved[orig].basereg = value[ra].reg;
1401 cache->reg_saved[orig].addr = value[ra].offset + imm;
1406 if (orig == NIOS2_EA_REGNUM || orig == NIOS2_ESTATUS_REGNUM)
1407 exception_handler = 1;
1410 /* Non-stack memory writes cannot appear in the prologue. */
1414 else if (nios2_match_stwm (insn, op, mach,
1415 ®list, &ra, &imm, &wb, &id))
1417 /* PUSH.N {reglist}, adjust
1419 STWM {reglist}, --(SP)[, writeback] */
1423 if (ra != NIOS2_SP_REGNUM || id != 0)
1424 /* This is a non-stack-push memory write and cannot be
1425 part of the prologue. */
1428 for (i = 31; i >= 0; i--)
1429 if (reglist & (1 << i))
1431 int orig = value[i].reg;
1434 if (orig > 0 && value[i].offset == 0 && pc < current_pc)
1436 cache->reg_saved[orig].basereg
1437 = value[NIOS2_SP_REGNUM].reg;
1438 cache->reg_saved[orig].addr
1439 = value[NIOS2_SP_REGNUM].offset - off;
1444 value[NIOS2_SP_REGNUM].offset -= off;
1445 value[NIOS2_SP_REGNUM].offset -= imm;
1450 else if (nios2_match_rdctl (insn, op, mach, &ra, &rc))
1453 This can appear in exception handlers in combination with
1454 a subsequent save to the stack frame. */
1457 value[rc].reg = NIOS2_STATUS_REGNUM + ra;
1458 value[rc].offset = 0;
1462 else if (nios2_match_calli (insn, op, mach, &uimm))
1464 if (value[8].reg == NIOS2_RA_REGNUM
1465 && value[8].offset == 0
1466 && value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM
1467 && value[NIOS2_SP_REGNUM].offset == 0)
1469 /* A CALL instruction. This is treated as a call to mcount
1470 if ra has been stored into r8 beforehand and if it's
1471 before the stack adjust.
1472 Note mcount corrupts r2-r3, r9-r15 & ra. */
1473 for (i = 2 ; i <= 3 ; i++)
1475 for (i = 9 ; i <= 15 ; i++)
1477 value[NIOS2_RA_REGNUM].reg = -1;
1482 /* Other calls are not part of the prologue. */
1487 else if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
1489 /* Branches not involving a stack overflow check aren't part of
1491 if (ra != NIOS2_SP_REGNUM)
1493 else if (cond == branch_geu)
1497 This instruction sequence is used in stack checking;
1498 we can ignore it. */
1499 unsigned int next_insn;
1500 const struct nios2_opcode *next_op
1501 = nios2_fetch_insn (gdbarch, pc, &next_insn);
1503 && (nios2_match_trap (next_insn, op, mach, &uimm)
1504 || nios2_match_break (next_insn, op, mach, &uimm)))
1505 pc += next_op->size;
1509 else if (cond == branch_ltu)
1511 /* BLTU sp, rx, .Lstackoverflow
1512 If the location branched to holds a TRAP or BREAK
1513 instruction then this is also stack overflow detection. */
1514 unsigned int next_insn;
1515 const struct nios2_opcode *next_op
1516 = nios2_fetch_insn (gdbarch, pc + imm, &next_insn);
1518 && (nios2_match_trap (next_insn, op, mach, &uimm)
1519 || nios2_match_break (next_insn, op, mach, &uimm)))
1528 /* All other calls, jumps, returns, TRAPs, or BREAKs terminate
1530 else if (nios2_match_callr (insn, op, mach, &ra)
1531 || nios2_match_jmpr (insn, op, mach, &ra)
1532 || nios2_match_jmpi (insn, op, mach, &uimm)
1533 || (nios2_match_ldwm (insn, op, mach, ®list, &ra,
1534 &imm, &wb, &id, &ret)
1536 || nios2_match_trap (insn, op, mach, &uimm)
1537 || nios2_match_break (insn, op, mach, &uimm))
1541 /* If THIS_FRAME is NULL, we are being called from skip_prologue
1542 and are only interested in the PROLOGUE_END value, so just
1543 return that now and skip over the cache updates, which depend
1544 on having frame information. */
1545 if (this_frame == NULL)
1546 return prologue_end;
1548 /* If we are in the function epilogue and have already popped
1549 registers off the stack in preparation for returning, then we
1550 want to go back to the original register values. */
1551 if (innermost && nios2_in_epilogue_p (gdbarch, current_pc, start_pc))
1552 nios2_setup_default (cache);
1554 /* Exception handlers use a different return address register. */
1555 if (exception_handler)
1556 cache->return_regnum = NIOS2_EA_REGNUM;
1559 fprintf_unfiltered (gdb_stdlog, "\n-> retreg=%d, ", cache->return_regnum);
1561 if (cache->reg_value[NIOS2_FP_REGNUM].reg == NIOS2_SP_REGNUM)
1562 /* If the FP now holds an offset from the CFA then this is a
1563 normal frame which uses the frame pointer. */
1564 base_reg = NIOS2_FP_REGNUM;
1565 else if (cache->reg_value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM)
1566 /* FP doesn't hold an offset from the CFA. If SP still holds an
1567 offset from the CFA then we might be in a function which omits
1568 the frame pointer, or we might be partway through the prologue.
1569 In both cases we can find the CFA using SP. */
1570 base_reg = NIOS2_SP_REGNUM;
1573 /* Somehow the stack pointer has been corrupted.
1576 fprintf_unfiltered (gdb_stdlog, "<can't reach cfa> }\n");
1580 if (cache->reg_value[base_reg].offset == 0
1581 || cache->reg_saved[NIOS2_RA_REGNUM].basereg != NIOS2_SP_REGNUM
1582 || cache->reg_saved[cache->return_regnum].basereg != NIOS2_SP_REGNUM)
1584 /* If the frame didn't adjust the stack, didn't save RA or
1585 didn't save EA in an exception handler then it must either
1586 be a leaf function (doesn't call any other functions) or it
1587 can't return. If it has called another function then it
1588 can't be a leaf, so set base == 0 to indicate that we can't
1589 backtrace past it. */
1593 /* If it isn't the innermost function then it can't be a
1594 leaf, unless it was interrupted. Check whether RA for
1595 this frame is the same as PC. If so then it probably
1596 wasn't interrupted. */
1598 = get_frame_register_unsigned (this_frame, NIOS2_RA_REGNUM);
1600 if (ra == current_pc)
1605 "<noreturn ADJUST %s, r31@r%d+?>, r%d@r%d+?> }\n",
1606 paddress (gdbarch, cache->reg_value[base_reg].offset),
1607 cache->reg_saved[NIOS2_RA_REGNUM].basereg,
1608 cache->return_regnum,
1609 cache->reg_saved[cache->return_regnum].basereg);
1615 /* Get the value of whichever register we are using for the
1617 cache->base = get_frame_register_unsigned (this_frame, base_reg);
1619 /* What was the value of SP at the start of this function (or just
1620 after the stack switch). */
1621 frame_high = cache->base - cache->reg_value[base_reg].offset;
1623 /* Adjust all the saved registers such that they contain addresses
1624 instead of offsets. */
1625 for (i = 0; i < NIOS2_NUM_REGS; i++)
1626 if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
1628 cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
1629 cache->reg_saved[i].addr += frame_high;
1632 for (i = 0; i < NIOS2_NUM_REGS; i++)
1633 if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
1635 CORE_ADDR gp = get_frame_register_unsigned (this_frame,
1638 for ( ; i < NIOS2_NUM_REGS; i++)
1639 if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
1641 cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
1642 cache->reg_saved[i].addr += gp;
1646 /* Work out what the value of SP was on the first instruction of
1647 this function. If we didn't switch stacks then this can be
1648 trivially computed from the base address. */
1649 if (cache->reg_saved[NIOS2_SP_REGNUM].basereg == NIOS2_Z_REGNUM)
1651 = read_memory_unsigned_integer (cache->reg_saved[NIOS2_SP_REGNUM].addr,
1654 cache->cfa = frame_high;
1656 /* Exception handlers restore ESTATUS into STATUS. */
1657 if (exception_handler)
1659 cache->reg_saved[NIOS2_STATUS_REGNUM]
1660 = cache->reg_saved[NIOS2_ESTATUS_REGNUM];
1661 cache->reg_saved[NIOS2_ESTATUS_REGNUM].basereg = -1;
1665 fprintf_unfiltered (gdb_stdlog, "cfa=%s }\n",
1666 paddress (gdbarch, cache->cfa));
1668 return prologue_end;
1671 /* Implement the skip_prologue gdbarch hook. */
1674 nios2_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1676 CORE_ADDR func_addr;
1678 struct nios2_unwind_cache cache;
1680 /* See if we can determine the end of the prologue via the symbol
1681 table. If so, then return either PC, or the PC after the
1682 prologue, whichever is greater. */
1683 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
1685 CORE_ADDR post_prologue_pc
1686 = skip_prologue_using_sal (gdbarch, func_addr);
1688 if (post_prologue_pc != 0)
1689 return std::max (start_pc, post_prologue_pc);
1692 /* Prologue analysis does the rest.... */
1693 nios2_init_cache (&cache, start_pc);
1694 return nios2_analyze_prologue (gdbarch, start_pc, start_pc, &cache, NULL);
1697 /* Implement the breakpoint_kind_from_pc gdbarch method. */
1700 nios2_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1702 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1704 if (mach == bfd_mach_nios2r2)
1707 const struct nios2_opcode *op
1708 = nios2_fetch_insn (gdbarch, *pcptr, &insn);
1710 if (op && op->size == NIOS2_CDX_OPCODE_SIZE)
1711 return NIOS2_CDX_OPCODE_SIZE;
1713 return NIOS2_OPCODE_SIZE;
1716 return NIOS2_OPCODE_SIZE;
1719 /* Implement the sw_breakpoint_from_kind gdbarch method. */
1721 static const gdb_byte *
1722 nios2_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1724 /* The Nios II ABI for Linux says: "Userspace programs should not use
1725 the break instruction and userspace debuggers should not insert
1726 one." and "Userspace breakpoints are accomplished using the trap
1727 instruction with immediate operand 31 (all ones)."
1729 So, we use "trap 31" consistently as the breakpoint on bare-metal
1730 as well as Linux targets. */
1732 /* R2 trap encoding:
1733 ((0x2d << 26) | (0x1f << 21) | (0x1d << 16) | (0x20 << 0))
1735 CDX trap.n encoding:
1736 ((0xd << 12) | (0x1f << 6) | (0x9 << 0))
1738 Note that code is always little-endian on R2. */
1741 if (kind == NIOS2_CDX_OPCODE_SIZE)
1743 static const gdb_byte cdx_breakpoint_le[] = {0xc9, 0xd7};
1745 return cdx_breakpoint_le;
1749 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1751 if (mach == bfd_mach_nios2r2)
1753 static const gdb_byte r2_breakpoint_le[] = {0x20, 0x00, 0xfd, 0xb7};
1755 return r2_breakpoint_le;
1759 enum bfd_endian byte_order_for_code
1760 = gdbarch_byte_order_for_code (gdbarch);
1761 /* R1 trap encoding:
1762 ((0x1d << 17) | (0x2d << 11) | (0x1f << 6) | (0x3a << 0))
1764 static const gdb_byte r1_breakpoint_le[] = {0xfa, 0x6f, 0x3b, 0x0};
1765 static const gdb_byte r1_breakpoint_be[] = {0x0, 0x3b, 0x6f, 0xfa};
1767 if (byte_order_for_code == BFD_ENDIAN_BIG)
1768 return r1_breakpoint_be;
1770 return r1_breakpoint_le;
1775 /* Implement the frame_align gdbarch method. */
1778 nios2_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1780 return align_down (addr, 4);
1784 /* Implement the return_value gdbarch method. */
1786 static enum return_value_convention
1787 nios2_return_value (struct gdbarch *gdbarch, struct value *function,
1788 struct type *type, struct regcache *regcache,
1789 gdb_byte *readbuf, const gdb_byte *writebuf)
1791 if (TYPE_LENGTH (type) > 8)
1792 return RETURN_VALUE_STRUCT_CONVENTION;
1795 nios2_extract_return_value (gdbarch, type, regcache, readbuf);
1797 nios2_store_return_value (gdbarch, type, regcache, writebuf);
1799 return RETURN_VALUE_REGISTER_CONVENTION;
1802 /* Implement the dummy_id gdbarch method. */
1804 static struct frame_id
1805 nios2_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1807 return frame_id_build
1808 (get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM),
1809 get_frame_pc (this_frame));
1812 /* Implement the push_dummy_call gdbarch method. */
1815 nios2_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1816 struct regcache *regcache, CORE_ADDR bp_addr,
1817 int nargs, struct value **args, CORE_ADDR sp,
1818 int struct_return, CORE_ADDR struct_addr)
1824 int stack_offset = 0;
1825 CORE_ADDR func_addr = find_function_addr (function, NULL);
1826 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1828 /* Set the return address register to point to the entry point of
1829 the program, where a breakpoint lies in wait. */
1830 regcache_cooked_write_signed (regcache, NIOS2_RA_REGNUM, bp_addr);
1832 /* Now make space on the stack for the args. */
1833 for (argnum = 0; argnum < nargs; argnum++)
1834 len += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);
1837 /* Initialize the register pointer. */
1838 argreg = NIOS2_FIRST_ARGREG;
1840 /* The struct_return pointer occupies the first parameter-passing
1843 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
1845 /* Now load as many as possible of the first arguments into
1846 registers, and push the rest onto the stack. Loop through args
1847 from first to last. */
1848 for (argnum = 0; argnum < nargs; argnum++)
1850 const gdb_byte *val;
1851 struct value *arg = args[argnum];
1852 struct type *arg_type = check_typedef (value_type (arg));
1853 int len = TYPE_LENGTH (arg_type);
1855 val = value_contents (arg);
1857 /* Copy the argument to general registers or the stack in
1858 register-sized pieces. Large arguments are split between
1859 registers and stack. */
1862 int partial_len = (len < 4 ? len : 4);
1864 if (argreg <= NIOS2_LAST_ARGREG)
1866 /* The argument is being passed in a register. */
1867 CORE_ADDR regval = extract_unsigned_integer (val, partial_len,
1870 regcache_cooked_write_unsigned (regcache, argreg, regval);
1875 /* The argument is being passed on the stack. */
1876 CORE_ADDR addr = sp + stack_offset;
1878 write_memory (addr, val, partial_len);
1879 stack_offset += align_up (partial_len, 4);
1887 regcache_cooked_write_signed (regcache, NIOS2_SP_REGNUM, sp);
1889 /* Return adjusted stack pointer. */
1893 /* Implement the unwind_pc gdbarch method. */
1896 nios2_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1900 frame_unwind_register (next_frame, NIOS2_PC_REGNUM, buf);
1901 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1904 /* Implement the unwind_sp gdbarch method. */
1907 nios2_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
1909 return frame_unwind_register_unsigned (this_frame, NIOS2_SP_REGNUM);
1912 /* Use prologue analysis to fill in the register cache
1913 *THIS_PROLOGUE_CACHE for THIS_FRAME. This function initializes
1914 *THIS_PROLOGUE_CACHE first. */
1916 static struct nios2_unwind_cache *
1917 nios2_frame_unwind_cache (struct frame_info *this_frame,
1918 void **this_prologue_cache)
1920 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1921 CORE_ADDR current_pc;
1922 struct nios2_unwind_cache *cache;
1924 if (*this_prologue_cache)
1925 return (struct nios2_unwind_cache *) *this_prologue_cache;
1927 cache = FRAME_OBSTACK_ZALLOC (struct nios2_unwind_cache);
1928 *this_prologue_cache = cache;
1930 /* Zero all fields. */
1931 nios2_init_cache (cache, get_frame_func (this_frame));
1933 /* Prologue analysis does the rest... */
1934 current_pc = get_frame_pc (this_frame);
1936 nios2_analyze_prologue (gdbarch, cache->pc, current_pc, cache, this_frame);
1941 /* Implement the this_id function for the normal unwinder. */
1944 nios2_frame_this_id (struct frame_info *this_frame, void **this_cache,
1945 struct frame_id *this_id)
1947 struct nios2_unwind_cache *cache =
1948 nios2_frame_unwind_cache (this_frame, this_cache);
1950 /* This marks the outermost frame. */
1951 if (cache->base == 0)
1954 *this_id = frame_id_build (cache->cfa, cache->pc);
1957 /* Implement the prev_register function for the normal unwinder. */
1959 static struct value *
1960 nios2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1963 struct nios2_unwind_cache *cache =
1964 nios2_frame_unwind_cache (this_frame, this_cache);
1966 gdb_assert (regnum >= 0 && regnum < NIOS2_NUM_REGS);
1968 /* The PC of the previous frame is stored in the RA register of
1969 the current frame. Frob regnum so that we pull the value from
1970 the correct place. */
1971 if (regnum == NIOS2_PC_REGNUM)
1972 regnum = cache->return_regnum;
1974 if (regnum == NIOS2_SP_REGNUM && cache->cfa)
1975 return frame_unwind_got_constant (this_frame, regnum, cache->cfa);
1977 /* If we've worked out where a register is stored then load it from
1979 if (cache->reg_saved[regnum].basereg == NIOS2_Z_REGNUM)
1980 return frame_unwind_got_memory (this_frame, regnum,
1981 cache->reg_saved[regnum].addr);
1983 return frame_unwind_got_register (this_frame, regnum, regnum);
1986 /* Implement the this_base, this_locals, and this_args hooks
1987 for the normal unwinder. */
1990 nios2_frame_base_address (struct frame_info *this_frame, void **this_cache)
1992 struct nios2_unwind_cache *info
1993 = nios2_frame_unwind_cache (this_frame, this_cache);
1998 /* Data structures for the normal prologue-analysis-based
2001 static const struct frame_unwind nios2_frame_unwind =
2004 default_frame_unwind_stop_reason,
2005 nios2_frame_this_id,
2006 nios2_frame_prev_register,
2008 default_frame_sniffer
2011 static const struct frame_base nios2_frame_base =
2013 &nios2_frame_unwind,
2014 nios2_frame_base_address,
2015 nios2_frame_base_address,
2016 nios2_frame_base_address
2019 /* Fill in the register cache *THIS_CACHE for THIS_FRAME for use
2020 in the stub unwinder. */
2022 static struct trad_frame_cache *
2023 nios2_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
2026 CORE_ADDR start_addr;
2027 CORE_ADDR stack_addr;
2028 struct trad_frame_cache *this_trad_cache;
2029 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2031 if (*this_cache != NULL)
2032 return (struct trad_frame_cache *) *this_cache;
2033 this_trad_cache = trad_frame_cache_zalloc (this_frame);
2034 *this_cache = this_trad_cache;
2036 /* The return address is in the link register. */
2037 trad_frame_set_reg_realreg (this_trad_cache,
2038 gdbarch_pc_regnum (gdbarch),
2041 /* Frame ID, since it's a frameless / stackless function, no stack
2042 space is allocated and SP on entry is the current SP. */
2043 pc = get_frame_pc (this_frame);
2044 find_pc_partial_function (pc, NULL, &start_addr, NULL);
2045 stack_addr = get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM);
2046 trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr));
2047 /* Assume that the frame's base is the same as the stack pointer. */
2048 trad_frame_set_this_base (this_trad_cache, stack_addr);
2050 return this_trad_cache;
2053 /* Implement the this_id function for the stub unwinder. */
2056 nios2_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
2057 struct frame_id *this_id)
2059 struct trad_frame_cache *this_trad_cache
2060 = nios2_stub_frame_cache (this_frame, this_cache);
2062 trad_frame_get_id (this_trad_cache, this_id);
2065 /* Implement the prev_register function for the stub unwinder. */
2067 static struct value *
2068 nios2_stub_frame_prev_register (struct frame_info *this_frame,
2069 void **this_cache, int regnum)
2071 struct trad_frame_cache *this_trad_cache
2072 = nios2_stub_frame_cache (this_frame, this_cache);
2074 return trad_frame_get_register (this_trad_cache, this_frame, regnum);
2077 /* Implement the sniffer function for the stub unwinder.
2078 This unwinder is used for cases where the normal
2079 prologue-analysis-based unwinder can't work,
2080 such as PLT stubs. */
2083 nios2_stub_frame_sniffer (const struct frame_unwind *self,
2084 struct frame_info *this_frame, void **cache)
2087 CORE_ADDR pc = get_frame_address_in_block (this_frame);
2089 /* Use the stub unwinder for unreadable code. */
2090 if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2093 if (in_plt_section (pc))
2099 /* Define the data structures for the stub unwinder. */
2101 static const struct frame_unwind nios2_stub_frame_unwind =
2104 default_frame_unwind_stop_reason,
2105 nios2_stub_frame_this_id,
2106 nios2_stub_frame_prev_register,
2108 nios2_stub_frame_sniffer
2113 /* Determine where to set a single step breakpoint while considering
2114 branch prediction. */
2117 nios2_get_next_pc (struct regcache *regcache, CORE_ADDR pc)
2119 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2120 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2121 unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
2123 const struct nios2_opcode *op = nios2_fetch_insn (gdbarch, pc, &insn);
2129 enum branch_condition cond;
2131 /* Do something stupid if we can't disassemble the insn at pc. */
2133 return pc + NIOS2_OPCODE_SIZE;
2135 if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
2137 int ras = regcache_raw_get_signed (regcache, ra);
2138 int rbs = regcache_raw_get_signed (regcache, rb);
2139 unsigned int rau = regcache_raw_get_unsigned (regcache, ra);
2140 unsigned int rbu = regcache_raw_get_unsigned (regcache, rb);
2177 else if (nios2_match_jmpi (insn, op, mach, &uimm)
2178 || nios2_match_calli (insn, op, mach, &uimm))
2179 pc = (pc & 0xf0000000) | uimm;
2181 else if (nios2_match_jmpr (insn, op, mach, &ra)
2182 || nios2_match_callr (insn, op, mach, &ra))
2183 pc = regcache_raw_get_unsigned (regcache, ra);
2185 else if (nios2_match_ldwm (insn, op, mach, &uimm, &ra, &imm, &wb, &id, &ret)
2188 /* If ra is in the reglist, we have to use the value saved in the
2189 stack frame rather than the current value. */
2190 if (uimm & (1 << NIOS2_RA_REGNUM))
2191 pc = nios2_unwind_pc (gdbarch, get_current_frame ());
2193 pc = regcache_raw_get_unsigned (regcache, NIOS2_RA_REGNUM);
2196 else if (nios2_match_trap (insn, op, mach, &uimm) && uimm == 0)
2198 if (tdep->syscall_next_pc != NULL)
2199 return tdep->syscall_next_pc (get_current_frame (), op);
2208 /* Implement the software_single_step gdbarch method. */
2210 static std::vector<CORE_ADDR>
2211 nios2_software_single_step (struct regcache *regcache)
2213 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2214 CORE_ADDR next_pc = nios2_get_next_pc (regcache, regcache_read_pc (regcache));
2219 /* Implement the get_longjump_target gdbarch method. */
2222 nios2_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
2224 struct gdbarch *gdbarch = get_frame_arch (frame);
2225 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2226 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2227 CORE_ADDR jb_addr = get_frame_register_unsigned (frame, NIOS2_R4_REGNUM);
2230 if (target_read_memory (jb_addr + (tdep->jb_pc * 4), buf, 4))
2233 *pc = extract_unsigned_integer (buf, 4, byte_order);
2237 /* Initialize the Nios II gdbarch. */
2239 static struct gdbarch *
2240 nios2_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2242 struct gdbarch *gdbarch;
2243 struct gdbarch_tdep *tdep;
2245 struct tdesc_arch_data *tdesc_data = NULL;
2246 const struct target_desc *tdesc = info.target_desc;
2248 if (!tdesc_has_registers (tdesc))
2249 /* Pick a default target description. */
2250 tdesc = tdesc_nios2;
2252 /* Check any target description for validity. */
2253 if (tdesc_has_registers (tdesc))
2255 const struct tdesc_feature *feature;
2258 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nios2.cpu");
2259 if (feature == NULL)
2262 tdesc_data = tdesc_data_alloc ();
2266 for (i = 0; i < NIOS2_NUM_REGS; i++)
2267 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
2268 nios2_reg_names[i]);
2272 tdesc_data_cleanup (tdesc_data);
2277 /* Find a candidate among the list of pre-declared architectures. */
2278 arches = gdbarch_list_lookup_by_info (arches, &info);
2280 return arches->gdbarch;
2282 /* None found, create a new architecture from the information
2284 tdep = XCNEW (struct gdbarch_tdep);
2285 gdbarch = gdbarch_alloc (&info, tdep);
2287 /* longjmp support not enabled by default. */
2290 /* Data type sizes. */
2291 set_gdbarch_ptr_bit (gdbarch, 32);
2292 set_gdbarch_addr_bit (gdbarch, 32);
2293 set_gdbarch_short_bit (gdbarch, 16);
2294 set_gdbarch_int_bit (gdbarch, 32);
2295 set_gdbarch_long_bit (gdbarch, 32);
2296 set_gdbarch_long_long_bit (gdbarch, 64);
2297 set_gdbarch_float_bit (gdbarch, 32);
2298 set_gdbarch_double_bit (gdbarch, 64);
2300 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2301 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2303 /* The register set. */
2304 set_gdbarch_num_regs (gdbarch, NIOS2_NUM_REGS);
2305 set_gdbarch_sp_regnum (gdbarch, NIOS2_SP_REGNUM);
2306 set_gdbarch_pc_regnum (gdbarch, NIOS2_PC_REGNUM); /* Pseudo register PC */
2308 set_gdbarch_register_name (gdbarch, nios2_register_name);
2309 set_gdbarch_register_type (gdbarch, nios2_register_type);
2311 /* Provide register mappings for stabs and dwarf2. */
2312 set_gdbarch_stab_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
2313 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
2315 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2317 /* Call dummy code. */
2318 set_gdbarch_frame_align (gdbarch, nios2_frame_align);
2320 set_gdbarch_return_value (gdbarch, nios2_return_value);
2322 set_gdbarch_skip_prologue (gdbarch, nios2_skip_prologue);
2323 set_gdbarch_stack_frame_destroyed_p (gdbarch, nios2_stack_frame_destroyed_p);
2324 set_gdbarch_breakpoint_kind_from_pc (gdbarch, nios2_breakpoint_kind_from_pc);
2325 set_gdbarch_sw_breakpoint_from_kind (gdbarch, nios2_sw_breakpoint_from_kind);
2327 set_gdbarch_dummy_id (gdbarch, nios2_dummy_id);
2328 set_gdbarch_unwind_pc (gdbarch, nios2_unwind_pc);
2329 set_gdbarch_unwind_sp (gdbarch, nios2_unwind_sp);
2331 /* The dwarf2 unwinder will normally produce the best results if
2332 the debug information is available, so register it first. */
2333 dwarf2_append_unwinders (gdbarch);
2334 frame_unwind_append_unwinder (gdbarch, &nios2_stub_frame_unwind);
2335 frame_unwind_append_unwinder (gdbarch, &nios2_frame_unwind);
2337 /* Single stepping. */
2338 set_gdbarch_software_single_step (gdbarch, nios2_software_single_step);
2340 /* Hook in ABI-specific overrides, if they have been registered. */
2341 gdbarch_init_osabi (info, gdbarch);
2343 if (tdep->jb_pc >= 0)
2344 set_gdbarch_get_longjmp_target (gdbarch, nios2_get_longjmp_target);
2346 frame_base_set_default (gdbarch, &nios2_frame_base);
2348 /* Enable inferior call support. */
2349 set_gdbarch_push_dummy_call (gdbarch, nios2_push_dummy_call);
2352 tdesc_use_registers (gdbarch, tdesc, tdesc_data);
2357 extern initialize_file_ftype _initialize_nios2_tdep; /* -Wmissing-prototypes */
2360 _initialize_nios2_tdep (void)
2362 gdbarch_register (bfd_arch_nios2, nios2_gdbarch_init, NULL);
2363 initialize_tdesc_nios2 ();
2365 /* Allow debugging this file's internals. */
2366 add_setshow_boolean_cmd ("nios2", class_maintenance, &nios2_debug,
2367 _("Set Nios II debugging."),
2368 _("Show Nios II debugging."),
2369 _("When on, Nios II specific debugging is enabled."),
2372 &setdebuglist, &showdebuglist);