]>
Commit | Line | Data |
---|---|---|
a1217d97 | 1 | /* Target-machine dependent code for Nios II, for GDB. |
ecd75fc8 | 2 | Copyright (C) 2012-2014 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 | ||
58 | static 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 | ||
67 | struct 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 | ||
79 | struct reg_saved | |
80 | { | |
81 | int basereg; | |
82 | CORE_ADDR addr; | |
83 | }; | |
84 | ||
85 | struct 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 | ||
110 | static 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 | ||
147 | static int | |
148 | nios2_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 | ||
161 | static 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 | ||
176 | static const char * | |
177 | nios2_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 | ||
188 | static struct type * | |
189 | nios2_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 | ||
207 | static void | |
208 | nios2_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 | ||
228 | static void | |
229 | nios2_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 | ||
249 | static void | |
250 | nios2_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 | ||
268 | static void | |
269 | nios2_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 | ||
282 | static const struct nios2_opcode * | |
283 | nios2_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 | ||
304 | static int | |
305 | nios2_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 | ||
321 | static int | |
322 | nios2_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 | ||
339 | static int | |
340 | nios2_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 | ||
357 | static int | |
358 | nios2_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 | ||
375 | static int | |
376 | nios2_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 | ||
393 | static int | |
394 | nios2_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 | ||
410 | static int | |
411 | nios2_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 | ||
428 | enum 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 | ||
438 | static int | |
439 | nios2_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 | ||
479 | static int | |
480 | nios2_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 | ||
495 | static int | |
496 | nios2_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 | ||
511 | static int | |
512 | nios2_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 | ||
537 | static int | |
538 | nios2_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 | ||
552 | static int | |
553 | nios2_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 | ||
567 | static int | |
568 | nios2_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 | |
586 | static int | |
587 | nios2_in_epilogue_p (struct gdbarch *gdbarch, | |
588 | CORE_ADDR current_pc, | |
589 | CORE_ADDR start_pc) | |
590 | { | |
d53c26c7 SL |
591 | unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach; |
592 | unsigned int insn; | |
593 | const struct nios2_opcode *op = NULL; | |
594 | unsigned int uimm; | |
595 | int imm; | |
596 | int ra, rb, rc; | |
597 | enum branch_condition cond; | |
a1217d97 SL |
598 | |
599 | /* There has to be a previous instruction in the function. */ | |
600 | if (current_pc > start_pc) | |
601 | { | |
d53c26c7 SL |
602 | int ok = 0; |
603 | ||
604 | /* Check whether the previous instruction was a stack adjustment. | |
605 | Possible instructions here include: | |
606 | ADDI sp, sp, n | |
607 | ADD sp, sp, rn | |
608 | LDW sp, n(sp) */ | |
609 | op = nios2_fetch_insn (gdbarch, current_pc - NIOS2_OPCODE_SIZE, &insn); | |
610 | if (op == NULL) | |
611 | return 0; | |
612 | ||
613 | /* Was it a stack adjustment? */ | |
614 | if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm)) | |
615 | ok = (rb == NIOS2_SP_REGNUM); | |
616 | else if (nios2_match_add (insn, op, mach, &ra, &rb, &rc)) | |
617 | ok = (rc == NIOS2_SP_REGNUM); | |
618 | else if (nios2_match_ldw (insn, op, mach, &ra, &rb, &imm)) | |
619 | ok = (rb == NIOS2_SP_REGNUM); | |
620 | if (!ok) | |
621 | return 0; | |
622 | ||
623 | /* Then check if it's followed by a return or a tail call. */ | |
624 | op = nios2_fetch_insn (gdbarch, current_pc, &insn); | |
625 | if (op == NULL) | |
626 | return 0; | |
627 | if (nios2_match_jmpr (insn, op, mach, &ra) | |
628 | || nios2_match_jmpi (insn, op, mach, &uimm) | |
629 | || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond) | |
630 | && cond == branch_none)) | |
631 | return 1; | |
a1217d97 SL |
632 | } |
633 | return 0; | |
634 | } | |
635 | ||
636 | /* Implement the in_function_epilogue_p gdbarch method. */ | |
637 | ||
638 | static int | |
639 | nios2_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc) | |
640 | { | |
641 | CORE_ADDR func_addr; | |
642 | ||
643 | if (find_pc_partial_function (pc, NULL, &func_addr, NULL)) | |
644 | return nios2_in_epilogue_p (gdbarch, pc, func_addr); | |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
a1217d97 SL |
649 | /* Do prologue analysis, returning the PC of the first instruction |
650 | after the function prologue. Assumes CACHE has already been | |
651 | initialized. THIS_FRAME can be null, in which case we are only | |
652 | interested in skipping the prologue. Otherwise CACHE is filled in | |
653 | from the frame information. | |
654 | ||
d53c26c7 SL |
655 | The prologue may consist of the following parts: |
656 | 1) Profiling instrumentation. For non-PIC code it looks like: | |
a1217d97 SL |
657 | mov r8, ra |
658 | call mcount | |
659 | mov ra, r8 | |
660 | ||
d53c26c7 SL |
661 | 2) A stack adjustment and save of R4-R7 for varargs functions. |
662 | This is typically merged with item 3. | |
663 | ||
664 | 3) A stack adjustment and save of the callee-saved registers; | |
665 | typically an explicit SP decrement and individual register | |
666 | saves. | |
667 | ||
668 | There may also be a stack switch here in an exception handler | |
669 | in place of a stack adjustment. It looks like: | |
a1217d97 SL |
670 | movhi rx, %hiadj(newstack) |
671 | addhi rx, rx, %lo(newstack) | |
672 | stw sp, constant(rx) | |
673 | mov sp, rx | |
674 | ||
d53c26c7 SL |
675 | 5) A frame pointer save, which can be either a MOV or ADDI. |
676 | ||
677 | 6) A further stack pointer adjustment. This is normally included | |
678 | adjustment in step 4 unless the total adjustment is too large | |
679 | to be done in one step. | |
680 | ||
681 | 7) A stack overflow check, which can take either of these forms: | |
a1217d97 SL |
682 | bgeu sp, rx, +8 |
683 | break 3 | |
684 | or | |
685 | bltu sp, rx, .Lstack_overflow | |
686 | ... | |
687 | .Lstack_overflow: | |
688 | break 3 | |
d53c26c7 SL |
689 | If present, this is inserted after the stack pointer adjustments |
690 | for steps 3, 4, and 6. | |
a1217d97 | 691 | |
d53c26c7 SL |
692 | The prologue instructions may be combined or interleaved with other |
693 | instructions. | |
a1217d97 SL |
694 | |
695 | To cope with all this variability we decode all the instructions | |
d53c26c7 SL |
696 | from the start of the prologue until we hit an instruction that |
697 | cannot possibly be a prologue instruction, such as a branch, call, | |
698 | return, or epilogue instruction. The prologue is considered to end | |
699 | at the last instruction that can definitely be considered a | |
700 | prologue instruction. */ | |
a1217d97 SL |
701 | |
702 | static CORE_ADDR | |
703 | nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc, | |
704 | const CORE_ADDR current_pc, | |
705 | struct nios2_unwind_cache *cache, | |
706 | struct frame_info *this_frame) | |
707 | { | |
d53c26c7 | 708 | /* Maximum number of possibly-prologue instructions to check. |
a1217d97 SL |
709 | Note that this number should not be too large, else we can |
710 | potentially end up iterating through unmapped memory. */ | |
d53c26c7 | 711 | int ninsns, max_insns = 50; |
a1217d97 SL |
712 | int regno; |
713 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
d53c26c7 | 714 | unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach; |
a1217d97 SL |
715 | |
716 | /* Does the frame set up the FP register? */ | |
717 | int base_reg = 0; | |
718 | ||
719 | struct reg_value *value = cache->reg_value; | |
720 | struct reg_value temp_value[NIOS2_NUM_REGS]; | |
721 | ||
722 | int i; | |
723 | ||
724 | /* Save the starting PC so we can correct the pc after running | |
725 | through the prolog, using symbol info. */ | |
726 | CORE_ADDR pc = start_pc; | |
727 | ||
728 | /* Is this an exception handler? */ | |
729 | int exception_handler = 0; | |
730 | ||
731 | /* What was the original value of SP (or fake original value for | |
732 | functions which switch stacks? */ | |
733 | CORE_ADDR frame_high; | |
734 | ||
d53c26c7 | 735 | /* The last definitely-prologue instruction seen. */ |
a1217d97 SL |
736 | CORE_ADDR prologue_end; |
737 | ||
738 | /* Is this the innermost function? */ | |
739 | int innermost = (this_frame ? (frame_relative_level (this_frame) == 0) : 1); | |
740 | ||
741 | if (nios2_debug) | |
742 | fprintf_unfiltered (gdb_stdlog, | |
743 | "{ nios2_analyze_prologue start=%s, current=%s ", | |
744 | paddress (gdbarch, start_pc), | |
745 | paddress (gdbarch, current_pc)); | |
746 | ||
747 | /* Set up the default values of the registers. */ | |
748 | nios2_setup_default (cache); | |
a1217d97 SL |
749 | |
750 | /* Find the prologue instructions. */ | |
d53c26c7 SL |
751 | prologue_end = start_pc; |
752 | for (ninsns = 0; ninsns < max_insns; ninsns++) | |
a1217d97 SL |
753 | { |
754 | /* Present instruction. */ | |
755 | uint32_t insn; | |
d53c26c7 SL |
756 | const struct nios2_opcode *op; |
757 | int ra, rb, rc, imm; | |
758 | unsigned int uimm; | |
759 | unsigned int reglist; | |
760 | int wb, ret; | |
761 | enum branch_condition cond; | |
a1217d97 SL |
762 | |
763 | if (pc == current_pc) | |
764 | { | |
765 | /* When we reach the current PC we must save the current | |
766 | register state (for the backtrace) but keep analysing | |
767 | because there might be more to find out (eg. is this an | |
768 | exception handler). */ | |
769 | memcpy (temp_value, value, sizeof (temp_value)); | |
770 | value = temp_value; | |
771 | if (nios2_debug) | |
772 | fprintf_unfiltered (gdb_stdlog, "*"); | |
773 | } | |
774 | ||
d53c26c7 SL |
775 | op = nios2_fetch_insn (gdbarch, pc, &insn); |
776 | ||
777 | /* Unknown opcode? Stop scanning. */ | |
778 | if (op == NULL) | |
779 | break; | |
780 | pc += op->size; | |
a1217d97 SL |
781 | |
782 | if (nios2_debug) | |
783 | fprintf_unfiltered (gdb_stdlog, "[%08X]", insn); | |
784 | ||
785 | /* The following instructions can appear in the prologue. */ | |
786 | ||
d53c26c7 | 787 | if (nios2_match_add (insn, op, mach, &ra, &rb, &rc)) |
a1217d97 SL |
788 | { |
789 | /* ADD rc, ra, rb (also used for MOV) */ | |
a1217d97 SL |
790 | if (rc == NIOS2_SP_REGNUM |
791 | && rb == 0 | |
792 | && value[ra].reg == cache->reg_saved[NIOS2_SP_REGNUM].basereg) | |
793 | { | |
794 | /* If the previous value of SP is available somewhere | |
795 | near the new stack pointer value then this is a | |
796 | stack switch. */ | |
797 | ||
798 | /* If any registers were saved on the stack before then | |
799 | we can't backtrace into them now. */ | |
800 | for (i = 0 ; i < NIOS2_NUM_REGS ; i++) | |
801 | { | |
802 | if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM) | |
803 | cache->reg_saved[i].basereg = -1; | |
804 | if (value[i].reg == NIOS2_SP_REGNUM) | |
805 | value[i].reg = -1; | |
806 | } | |
807 | ||
808 | /* Create a fake "high water mark" 4 bytes above where SP | |
809 | was stored and fake up the registers to be consistent | |
810 | with that. */ | |
811 | value[NIOS2_SP_REGNUM].reg = NIOS2_SP_REGNUM; | |
812 | value[NIOS2_SP_REGNUM].offset | |
813 | = (value[ra].offset | |
814 | - cache->reg_saved[NIOS2_SP_REGNUM].addr | |
815 | - 4); | |
816 | cache->reg_saved[NIOS2_SP_REGNUM].basereg = NIOS2_SP_REGNUM; | |
817 | cache->reg_saved[NIOS2_SP_REGNUM].addr = -4; | |
818 | } | |
819 | ||
820 | else if (rc != 0) | |
821 | { | |
822 | if (value[rb].reg == 0) | |
823 | value[rc].reg = value[ra].reg; | |
824 | else if (value[ra].reg == 0) | |
825 | value[rc].reg = value[rb].reg; | |
826 | else | |
827 | value[rc].reg = -1; | |
828 | value[rc].offset = value[ra].offset + value[rb].offset; | |
829 | } | |
a1217d97 | 830 | |
d53c26c7 SL |
831 | prologue_end = pc; |
832 | } | |
833 | ||
834 | else if (nios2_match_sub (insn, op, mach, &ra, &rb, &rc)) | |
a1217d97 SL |
835 | { |
836 | /* SUB rc, ra, rb */ | |
a1217d97 SL |
837 | if (rc != 0) |
838 | { | |
839 | if (value[rb].reg == 0) | |
840 | value[rc].reg = value[ra].reg; | |
841 | else | |
842 | value[rc].reg = -1; | |
843 | value[rc].offset = value[ra].offset - value[rb].offset; | |
844 | } | |
845 | } | |
846 | ||
d53c26c7 | 847 | else if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm)) |
a1217d97 | 848 | { |
d53c26c7 | 849 | /* ADDI rb, ra, imm */ |
a1217d97 SL |
850 | |
851 | /* The first stack adjustment is part of the prologue. | |
852 | Any subsequent stack adjustments are either down to | |
853 | alloca or the epilogue so stop analysing when we hit | |
854 | them. */ | |
855 | if (rb == NIOS2_SP_REGNUM | |
856 | && (value[rb].offset != 0 || value[ra].reg != NIOS2_SP_REGNUM)) | |
857 | break; | |
858 | ||
859 | if (rb != 0) | |
860 | { | |
861 | value[rb].reg = value[ra].reg; | |
d53c26c7 | 862 | value[rb].offset = value[ra].offset + imm; |
a1217d97 SL |
863 | } |
864 | ||
d53c26c7 | 865 | prologue_end = pc; |
a1217d97 SL |
866 | } |
867 | ||
d53c26c7 | 868 | else if (nios2_match_orhi (insn, op, mach, &ra, &rb, &uimm)) |
a1217d97 | 869 | { |
d53c26c7 | 870 | /* ORHI rb, ra, uimm (also used for MOVHI) */ |
a1217d97 SL |
871 | if (rb != 0) |
872 | { | |
873 | value[rb].reg = (value[ra].reg == 0) ? 0 : -1; | |
d53c26c7 | 874 | value[rb].offset = value[ra].offset | (uimm << 16); |
a1217d97 SL |
875 | } |
876 | } | |
877 | ||
d53c26c7 | 878 | else if (nios2_match_stw (insn, op, mach, &ra, &rb, &imm)) |
a1217d97 | 879 | { |
d53c26c7 | 880 | /* STW rb, imm(ra) */ |
a1217d97 | 881 | |
d53c26c7 | 882 | /* Are we storing the original value of a register to the stack? |
a1217d97 SL |
883 | For exception handlers the value of EA-4 (return |
884 | address from interrupts etc) is sometimes stored. */ | |
885 | int orig = value[rb].reg; | |
886 | if (orig > 0 | |
887 | && (value[rb].offset == 0 | |
d53c26c7 SL |
888 | || (orig == NIOS2_EA_REGNUM && value[rb].offset == -4)) |
889 | && ((value[ra].reg == NIOS2_SP_REGNUM | |
a1217d97 | 890 | && cache->reg_saved[orig].basereg != NIOS2_SP_REGNUM) |
d53c26c7 SL |
891 | || cache->reg_saved[orig].basereg == -1)) |
892 | { | |
893 | if (pc < current_pc) | |
a1217d97 | 894 | { |
d53c26c7 SL |
895 | /* Save off callee saved registers. */ |
896 | cache->reg_saved[orig].basereg = value[ra].reg; | |
897 | cache->reg_saved[orig].addr = value[ra].offset + imm; | |
a1217d97 | 898 | } |
d53c26c7 SL |
899 | |
900 | prologue_end = pc; | |
901 | ||
902 | if (orig == NIOS2_EA_REGNUM || orig == NIOS2_ESTATUS_REGNUM) | |
903 | exception_handler = 1; | |
a1217d97 SL |
904 | } |
905 | else | |
d53c26c7 SL |
906 | /* Non-stack memory writes cannot appear in the prologue. */ |
907 | break; | |
a1217d97 SL |
908 | } |
909 | ||
d53c26c7 | 910 | else if (nios2_match_rdctl (insn, op, mach, &ra, &rc)) |
a1217d97 | 911 | { |
d53c26c7 SL |
912 | /* RDCTL rC, ctlN |
913 | This can appear in exception handlers in combination with | |
914 | a subsequent save to the stack frame. */ | |
a1217d97 SL |
915 | if (rc != 0) |
916 | { | |
d53c26c7 | 917 | value[rc].reg = NIOS2_STATUS_REGNUM + ra; |
a1217d97 SL |
918 | value[rc].offset = 0; |
919 | } | |
a1217d97 SL |
920 | } |
921 | ||
d53c26c7 | 922 | else if (nios2_match_calli (insn, op, mach, &uimm)) |
a1217d97 | 923 | { |
d53c26c7 SL |
924 | if (value[8].reg == NIOS2_RA_REGNUM |
925 | && value[8].offset == 0 | |
926 | && value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM | |
927 | && value[NIOS2_SP_REGNUM].offset == 0) | |
928 | { | |
929 | /* A CALL instruction. This is treated as a call to mcount | |
930 | if ra has been stored into r8 beforehand and if it's | |
931 | before the stack adjust. | |
932 | Note mcount corrupts r2-r3, r9-r15 & ra. */ | |
933 | for (i = 2 ; i <= 3 ; i++) | |
934 | value[i].reg = -1; | |
935 | for (i = 9 ; i <= 15 ; i++) | |
936 | value[i].reg = -1; | |
937 | value[NIOS2_RA_REGNUM].reg = -1; | |
938 | ||
939 | prologue_end = pc; | |
940 | } | |
a1217d97 | 941 | |
d53c26c7 | 942 | /* Other calls are not part of the prologue. */ |
a1217d97 | 943 | else |
d53c26c7 | 944 | break; |
a1217d97 SL |
945 | } |
946 | ||
d53c26c7 | 947 | else if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)) |
a1217d97 | 948 | { |
d53c26c7 SL |
949 | /* Branches not involving a stack overflow check aren't part of |
950 | the prologue. */ | |
951 | if (ra != NIOS2_SP_REGNUM) | |
952 | break; | |
953 | else if (cond == branch_geu) | |
a1217d97 | 954 | { |
d53c26c7 SL |
955 | /* BGEU sp, rx, +8 |
956 | BREAK 3 | |
957 | This instruction sequence is used in stack checking; | |
958 | we can ignore it. */ | |
959 | unsigned int next_insn; | |
960 | const struct nios2_opcode *next_op | |
961 | = nios2_fetch_insn (gdbarch, pc, &next_insn); | |
962 | if (next_op != NULL | |
963 | && nios2_match_break (next_insn, op, mach, &uimm)) | |
964 | pc += next_op->size; | |
965 | else | |
966 | break; | |
a1217d97 | 967 | } |
d53c26c7 SL |
968 | else if (cond == branch_ltu) |
969 | { | |
970 | /* BLTU sp, rx, .Lstackoverflow | |
971 | If the location branched to holds a BREAK 3 instruction | |
972 | then this is also stack overflow detection. */ | |
973 | unsigned int next_insn; | |
974 | const struct nios2_opcode *next_op | |
975 | = nios2_fetch_insn (gdbarch, pc + imm, &next_insn); | |
976 | if (next_op != NULL | |
977 | && nios2_match_break (next_insn, op, mach, &uimm)) | |
978 | ; | |
979 | else | |
980 | break; | |
981 | } | |
982 | else | |
983 | break; | |
a1217d97 SL |
984 | } |
985 | ||
d53c26c7 SL |
986 | /* All other calls or jumps (including returns) terminate |
987 | the prologue. */ | |
988 | else if (nios2_match_callr (insn, op, mach, &ra) | |
989 | || nios2_match_jmpr (insn, op, mach, &ra) | |
990 | || nios2_match_jmpi (insn, op, mach, &uimm)) | |
991 | break; | |
a1217d97 SL |
992 | } |
993 | ||
994 | /* If THIS_FRAME is NULL, we are being called from skip_prologue | |
995 | and are only interested in the PROLOGUE_END value, so just | |
996 | return that now and skip over the cache updates, which depend | |
997 | on having frame information. */ | |
998 | if (this_frame == NULL) | |
999 | return prologue_end; | |
1000 | ||
1001 | /* If we are in the function epilogue and have already popped | |
1002 | registers off the stack in preparation for returning, then we | |
1003 | want to go back to the original register values. */ | |
1004 | if (innermost && nios2_in_epilogue_p (gdbarch, current_pc, start_pc)) | |
1005 | nios2_setup_default (cache); | |
1006 | ||
1007 | /* Exception handlers use a different return address register. */ | |
1008 | if (exception_handler) | |
1009 | cache->return_regnum = NIOS2_EA_REGNUM; | |
1010 | ||
1011 | if (nios2_debug) | |
1012 | fprintf_unfiltered (gdb_stdlog, "\n-> retreg=%d, ", cache->return_regnum); | |
1013 | ||
1014 | if (cache->reg_value[NIOS2_FP_REGNUM].reg == NIOS2_SP_REGNUM) | |
1015 | /* If the FP now holds an offset from the CFA then this is a | |
1016 | normal frame which uses the frame pointer. */ | |
1017 | base_reg = NIOS2_FP_REGNUM; | |
1018 | else if (cache->reg_value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM) | |
1019 | /* FP doesn't hold an offset from the CFA. If SP still holds an | |
1020 | offset from the CFA then we might be in a function which omits | |
1021 | the frame pointer, or we might be partway through the prologue. | |
1022 | In both cases we can find the CFA using SP. */ | |
1023 | base_reg = NIOS2_SP_REGNUM; | |
1024 | else | |
1025 | { | |
1026 | /* Somehow the stack pointer has been corrupted. | |
1027 | We can't return. */ | |
1028 | if (nios2_debug) | |
1029 | fprintf_unfiltered (gdb_stdlog, "<can't reach cfa> }\n"); | |
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | if (cache->reg_value[base_reg].offset == 0 | |
1034 | || cache->reg_saved[NIOS2_RA_REGNUM].basereg != NIOS2_SP_REGNUM | |
1035 | || cache->reg_saved[cache->return_regnum].basereg != NIOS2_SP_REGNUM) | |
1036 | { | |
1037 | /* If the frame didn't adjust the stack, didn't save RA or | |
1038 | didn't save EA in an exception handler then it must either | |
1039 | be a leaf function (doesn't call any other functions) or it | |
1040 | can't return. If it has called another function then it | |
1041 | can't be a leaf, so set base == 0 to indicate that we can't | |
1042 | backtrace past it. */ | |
1043 | ||
1044 | if (!innermost) | |
1045 | { | |
1046 | /* If it isn't the innermost function then it can't be a | |
1047 | leaf, unless it was interrupted. Check whether RA for | |
1048 | this frame is the same as PC. If so then it probably | |
1049 | wasn't interrupted. */ | |
1050 | CORE_ADDR ra | |
1051 | = get_frame_register_unsigned (this_frame, NIOS2_RA_REGNUM); | |
1052 | ||
1053 | if (ra == current_pc) | |
1054 | { | |
1055 | if (nios2_debug) | |
1056 | fprintf_unfiltered | |
1057 | (gdb_stdlog, | |
1058 | "<noreturn ADJUST %s, r31@r%d+?>, r%d@r%d+?> }\n", | |
1059 | paddress (gdbarch, cache->reg_value[base_reg].offset), | |
1060 | cache->reg_saved[NIOS2_RA_REGNUM].basereg, | |
1061 | cache->return_regnum, | |
1062 | cache->reg_saved[cache->return_regnum].basereg); | |
1063 | return 0; | |
1064 | } | |
1065 | } | |
1066 | } | |
1067 | ||
1068 | /* Get the value of whichever register we are using for the | |
1069 | base. */ | |
1070 | cache->base = get_frame_register_unsigned (this_frame, base_reg); | |
1071 | ||
1072 | /* What was the value of SP at the start of this function (or just | |
1073 | after the stack switch). */ | |
1074 | frame_high = cache->base - cache->reg_value[base_reg].offset; | |
1075 | ||
1076 | /* Adjust all the saved registers such that they contain addresses | |
1077 | instead of offsets. */ | |
1078 | for (i = 0; i < NIOS2_NUM_REGS; i++) | |
1079 | if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM) | |
1080 | { | |
1081 | cache->reg_saved[i].basereg = NIOS2_Z_REGNUM; | |
1082 | cache->reg_saved[i].addr += frame_high; | |
1083 | } | |
1084 | ||
1085 | for (i = 0; i < NIOS2_NUM_REGS; i++) | |
1086 | if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM) | |
1087 | { | |
1088 | CORE_ADDR gp = get_frame_register_unsigned (this_frame, | |
1089 | NIOS2_GP_REGNUM); | |
1090 | ||
1091 | for ( ; i < NIOS2_NUM_REGS; i++) | |
1092 | if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM) | |
1093 | { | |
1094 | cache->reg_saved[i].basereg = NIOS2_Z_REGNUM; | |
1095 | cache->reg_saved[i].addr += gp; | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | /* Work out what the value of SP was on the first instruction of | |
1100 | this function. If we didn't switch stacks then this can be | |
1101 | trivially computed from the base address. */ | |
1102 | if (cache->reg_saved[NIOS2_SP_REGNUM].basereg == NIOS2_Z_REGNUM) | |
1103 | cache->cfa | |
1104 | = read_memory_unsigned_integer (cache->reg_saved[NIOS2_SP_REGNUM].addr, | |
1105 | 4, byte_order); | |
1106 | else | |
1107 | cache->cfa = frame_high; | |
1108 | ||
1109 | /* Exception handlers restore ESTATUS into STATUS. */ | |
1110 | if (exception_handler) | |
1111 | { | |
1112 | cache->reg_saved[NIOS2_STATUS_REGNUM] | |
1113 | = cache->reg_saved[NIOS2_ESTATUS_REGNUM]; | |
1114 | cache->reg_saved[NIOS2_ESTATUS_REGNUM].basereg = -1; | |
1115 | } | |
1116 | ||
1117 | if (nios2_debug) | |
1118 | fprintf_unfiltered (gdb_stdlog, "cfa=%s }\n", | |
1119 | paddress (gdbarch, cache->cfa)); | |
1120 | ||
1121 | return prologue_end; | |
1122 | } | |
1123 | ||
1124 | /* Implement the skip_prologue gdbarch hook. */ | |
1125 | ||
1126 | static CORE_ADDR | |
1127 | nios2_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc) | |
1128 | { | |
a1217d97 SL |
1129 | CORE_ADDR func_addr; |
1130 | ||
1131 | struct nios2_unwind_cache cache; | |
1132 | ||
1133 | /* See if we can determine the end of the prologue via the symbol | |
1134 | table. If so, then return either PC, or the PC after the | |
1135 | prologue, whichever is greater. */ | |
1136 | if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL)) | |
1137 | { | |
1138 | CORE_ADDR post_prologue_pc | |
1139 | = skip_prologue_using_sal (gdbarch, func_addr); | |
1140 | ||
1141 | if (post_prologue_pc != 0) | |
1142 | return max (start_pc, post_prologue_pc); | |
1143 | } | |
1144 | ||
1145 | /* Prologue analysis does the rest.... */ | |
1146 | nios2_init_cache (&cache, start_pc); | |
1147 | return nios2_analyze_prologue (gdbarch, start_pc, start_pc, &cache, NULL); | |
1148 | } | |
1149 | ||
1150 | /* Implement the breakpoint_from_pc gdbarch hook. */ | |
1151 | ||
1152 | static const gdb_byte* | |
1153 | nios2_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *bp_addr, | |
1154 | int *bp_size) | |
1155 | { | |
a1217d97 | 1156 | enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch); |
d53c26c7 SL |
1157 | unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach; |
1158 | ||
1159 | /* R1 break encoding: | |
1160 | ((0x1e << 17) | (0x34 << 11) | (0x1f << 6) | (0x3a << 0)) | |
1161 | 0x003da7fa */ | |
1162 | static const gdb_byte r1_breakpoint_le[] = {0xfa, 0xa7, 0x3d, 0x0}; | |
1163 | static const gdb_byte r1_breakpoint_be[] = {0x0, 0x3d, 0xa7, 0xfa}; | |
1164 | *bp_size = NIOS2_OPCODE_SIZE; | |
1165 | if (byte_order_for_code == BFD_ENDIAN_BIG) | |
1166 | return r1_breakpoint_be; | |
a1217d97 | 1167 | else |
d53c26c7 | 1168 | return r1_breakpoint_le; |
a1217d97 SL |
1169 | } |
1170 | ||
1171 | /* Implement the print_insn gdbarch method. */ | |
1172 | ||
1173 | static int | |
1174 | nios2_print_insn (bfd_vma memaddr, disassemble_info *info) | |
1175 | { | |
1176 | if (info->endian == BFD_ENDIAN_BIG) | |
1177 | return print_insn_big_nios2 (memaddr, info); | |
1178 | else | |
1179 | return print_insn_little_nios2 (memaddr, info); | |
1180 | } | |
1181 | ||
1182 | ||
1183 | /* Implement the frame_align gdbarch method. */ | |
1184 | ||
1185 | static CORE_ADDR | |
1186 | nios2_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr) | |
1187 | { | |
1188 | return align_down (addr, 4); | |
1189 | } | |
1190 | ||
1191 | ||
1192 | /* Implement the return_value gdbarch method. */ | |
1193 | ||
1194 | static enum return_value_convention | |
1195 | nios2_return_value (struct gdbarch *gdbarch, struct value *function, | |
1196 | struct type *type, struct regcache *regcache, | |
1197 | gdb_byte *readbuf, const gdb_byte *writebuf) | |
1198 | { | |
1199 | if (TYPE_LENGTH (type) > 8) | |
1200 | return RETURN_VALUE_STRUCT_CONVENTION; | |
1201 | ||
1202 | if (readbuf) | |
1203 | nios2_extract_return_value (gdbarch, type, regcache, readbuf); | |
1204 | if (writebuf) | |
1205 | nios2_store_return_value (gdbarch, type, regcache, writebuf); | |
1206 | ||
1207 | return RETURN_VALUE_REGISTER_CONVENTION; | |
1208 | } | |
1209 | ||
1210 | /* Implement the dummy_id gdbarch method. */ | |
1211 | ||
1212 | static struct frame_id | |
1213 | nios2_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame) | |
1214 | { | |
1215 | return frame_id_build | |
1216 | (get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM), | |
1217 | get_frame_pc (this_frame)); | |
1218 | } | |
1219 | ||
1220 | /* Implement the push_dummy_call gdbarch method. */ | |
1221 | ||
1222 | static CORE_ADDR | |
1223 | nios2_push_dummy_call (struct gdbarch *gdbarch, struct value *function, | |
1224 | struct regcache *regcache, CORE_ADDR bp_addr, | |
1225 | int nargs, struct value **args, CORE_ADDR sp, | |
1226 | int struct_return, CORE_ADDR struct_addr) | |
1227 | { | |
1228 | int argreg; | |
1229 | int float_argreg; | |
1230 | int argnum; | |
1231 | int len = 0; | |
1232 | int stack_offset = 0; | |
1233 | CORE_ADDR func_addr = find_function_addr (function, NULL); | |
1234 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
1235 | ||
1236 | /* Set the return address register to point to the entry point of | |
1237 | the program, where a breakpoint lies in wait. */ | |
1238 | regcache_cooked_write_signed (regcache, NIOS2_RA_REGNUM, bp_addr); | |
1239 | ||
1240 | /* Now make space on the stack for the args. */ | |
1241 | for (argnum = 0; argnum < nargs; argnum++) | |
1242 | len += align_up (TYPE_LENGTH (value_type (args[argnum])), 4); | |
1243 | sp -= len; | |
1244 | ||
1245 | /* Initialize the register pointer. */ | |
1246 | argreg = NIOS2_FIRST_ARGREG; | |
1247 | ||
1248 | /* The struct_return pointer occupies the first parameter-passing | |
1249 | register. */ | |
1250 | if (struct_return) | |
1251 | regcache_cooked_write_unsigned (regcache, argreg++, struct_addr); | |
1252 | ||
1253 | /* Now load as many as possible of the first arguments into | |
1254 | registers, and push the rest onto the stack. Loop through args | |
1255 | from first to last. */ | |
1256 | for (argnum = 0; argnum < nargs; argnum++) | |
1257 | { | |
1258 | const gdb_byte *val; | |
1259 | gdb_byte valbuf[MAX_REGISTER_SIZE]; | |
1260 | struct value *arg = args[argnum]; | |
1261 | struct type *arg_type = check_typedef (value_type (arg)); | |
1262 | int len = TYPE_LENGTH (arg_type); | |
1263 | enum type_code typecode = TYPE_CODE (arg_type); | |
1264 | ||
1265 | val = value_contents (arg); | |
1266 | ||
1267 | /* Copy the argument to general registers or the stack in | |
1268 | register-sized pieces. Large arguments are split between | |
1269 | registers and stack. */ | |
1270 | while (len > 0) | |
1271 | { | |
1272 | int partial_len = (len < 4 ? len : 4); | |
1273 | ||
1274 | if (argreg <= NIOS2_LAST_ARGREG) | |
1275 | { | |
1276 | /* The argument is being passed in a register. */ | |
1277 | CORE_ADDR regval = extract_unsigned_integer (val, partial_len, | |
1278 | byte_order); | |
1279 | ||
1280 | regcache_cooked_write_unsigned (regcache, argreg, regval); | |
1281 | argreg++; | |
1282 | } | |
1283 | else | |
1284 | { | |
1285 | /* The argument is being passed on the stack. */ | |
1286 | CORE_ADDR addr = sp + stack_offset; | |
1287 | ||
1288 | write_memory (addr, val, partial_len); | |
1289 | stack_offset += align_up (partial_len, 4); | |
1290 | } | |
1291 | ||
1292 | len -= partial_len; | |
1293 | val += partial_len; | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | regcache_cooked_write_signed (regcache, NIOS2_SP_REGNUM, sp); | |
1298 | ||
1299 | /* Return adjusted stack pointer. */ | |
1300 | return sp; | |
1301 | } | |
1302 | ||
1303 | /* Implement the unwind_pc gdbarch method. */ | |
1304 | ||
1305 | static CORE_ADDR | |
1306 | nios2_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
1307 | { | |
1308 | gdb_byte buf[4]; | |
1309 | ||
1310 | frame_unwind_register (next_frame, NIOS2_PC_REGNUM, buf); | |
1311 | return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr); | |
1312 | } | |
1313 | ||
1314 | /* Implement the unwind_sp gdbarch method. */ | |
1315 | ||
1316 | static CORE_ADDR | |
1317 | nios2_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame) | |
1318 | { | |
1319 | return frame_unwind_register_unsigned (this_frame, NIOS2_SP_REGNUM); | |
1320 | } | |
1321 | ||
1322 | /* Use prologue analysis to fill in the register cache | |
1323 | *THIS_PROLOGUE_CACHE for THIS_FRAME. This function initializes | |
1324 | *THIS_PROLOGUE_CACHE first. */ | |
1325 | ||
1326 | static struct nios2_unwind_cache * | |
1327 | nios2_frame_unwind_cache (struct frame_info *this_frame, | |
1328 | void **this_prologue_cache) | |
1329 | { | |
1330 | struct gdbarch *gdbarch = get_frame_arch (this_frame); | |
1331 | CORE_ADDR current_pc; | |
1332 | struct nios2_unwind_cache *cache; | |
1333 | int i; | |
1334 | ||
1335 | if (*this_prologue_cache) | |
1336 | return *this_prologue_cache; | |
1337 | ||
1338 | cache = FRAME_OBSTACK_ZALLOC (struct nios2_unwind_cache); | |
1339 | *this_prologue_cache = cache; | |
1340 | ||
1341 | /* Zero all fields. */ | |
1342 | nios2_init_cache (cache, get_frame_func (this_frame)); | |
1343 | ||
1344 | /* Prologue analysis does the rest... */ | |
1345 | current_pc = get_frame_pc (this_frame); | |
1346 | if (cache->pc != 0) | |
1347 | nios2_analyze_prologue (gdbarch, cache->pc, current_pc, cache, this_frame); | |
1348 | ||
1349 | return cache; | |
1350 | } | |
1351 | ||
1352 | /* Implement the this_id function for the normal unwinder. */ | |
1353 | ||
1354 | static void | |
1355 | nios2_frame_this_id (struct frame_info *this_frame, void **this_cache, | |
1356 | struct frame_id *this_id) | |
1357 | { | |
1358 | struct nios2_unwind_cache *cache = | |
1359 | nios2_frame_unwind_cache (this_frame, this_cache); | |
1360 | ||
1361 | /* This marks the outermost frame. */ | |
1362 | if (cache->base == 0) | |
1363 | return; | |
1364 | ||
1365 | *this_id = frame_id_build (cache->cfa, cache->pc); | |
1366 | } | |
1367 | ||
1368 | /* Implement the prev_register function for the normal unwinder. */ | |
1369 | ||
1370 | static struct value * | |
1371 | nios2_frame_prev_register (struct frame_info *this_frame, void **this_cache, | |
1372 | int regnum) | |
1373 | { | |
1374 | struct nios2_unwind_cache *cache = | |
1375 | nios2_frame_unwind_cache (this_frame, this_cache); | |
1376 | ||
1377 | gdb_assert (regnum >= 0 && regnum < NIOS2_NUM_REGS); | |
1378 | ||
1379 | /* The PC of the previous frame is stored in the RA register of | |
1380 | the current frame. Frob regnum so that we pull the value from | |
1381 | the correct place. */ | |
1382 | if (regnum == NIOS2_PC_REGNUM) | |
1383 | regnum = cache->return_regnum; | |
1384 | ||
1385 | if (regnum == NIOS2_SP_REGNUM && cache->cfa) | |
1386 | return frame_unwind_got_constant (this_frame, regnum, cache->cfa); | |
1387 | ||
1388 | /* If we've worked out where a register is stored then load it from | |
1389 | there. */ | |
1390 | if (cache->reg_saved[regnum].basereg == NIOS2_Z_REGNUM) | |
1391 | return frame_unwind_got_memory (this_frame, regnum, | |
1392 | cache->reg_saved[regnum].addr); | |
1393 | ||
1394 | return frame_unwind_got_register (this_frame, regnum, regnum); | |
1395 | } | |
1396 | ||
1397 | /* Implement the this_base, this_locals, and this_args hooks | |
1398 | for the normal unwinder. */ | |
1399 | ||
1400 | static CORE_ADDR | |
1401 | nios2_frame_base_address (struct frame_info *this_frame, void **this_cache) | |
1402 | { | |
1403 | struct nios2_unwind_cache *info | |
1404 | = nios2_frame_unwind_cache (this_frame, this_cache); | |
1405 | ||
1406 | return info->base; | |
1407 | } | |
1408 | ||
1409 | /* Data structures for the normal prologue-analysis-based | |
1410 | unwinder. */ | |
1411 | ||
1412 | static const struct frame_unwind nios2_frame_unwind = | |
1413 | { | |
1414 | NORMAL_FRAME, | |
1415 | default_frame_unwind_stop_reason, | |
1416 | nios2_frame_this_id, | |
1417 | nios2_frame_prev_register, | |
1418 | NULL, | |
1419 | default_frame_sniffer | |
1420 | }; | |
1421 | ||
1422 | static const struct frame_base nios2_frame_base = | |
1423 | { | |
1424 | &nios2_frame_unwind, | |
1425 | nios2_frame_base_address, | |
1426 | nios2_frame_base_address, | |
1427 | nios2_frame_base_address | |
1428 | }; | |
1429 | ||
1430 | /* Fill in the register cache *THIS_CACHE for THIS_FRAME for use | |
1431 | in the stub unwinder. */ | |
1432 | ||
1433 | static struct trad_frame_cache * | |
1434 | nios2_stub_frame_cache (struct frame_info *this_frame, void **this_cache) | |
1435 | { | |
1436 | CORE_ADDR pc; | |
1437 | CORE_ADDR start_addr; | |
1438 | CORE_ADDR stack_addr; | |
1439 | struct trad_frame_cache *this_trad_cache; | |
1440 | struct gdbarch *gdbarch = get_frame_arch (this_frame); | |
1441 | int num_regs = gdbarch_num_regs (gdbarch); | |
1442 | ||
1443 | if (*this_cache != NULL) | |
1444 | return *this_cache; | |
1445 | this_trad_cache = trad_frame_cache_zalloc (this_frame); | |
1446 | *this_cache = this_trad_cache; | |
1447 | ||
1448 | /* The return address is in the link register. */ | |
1449 | trad_frame_set_reg_realreg (this_trad_cache, | |
1450 | gdbarch_pc_regnum (gdbarch), | |
1451 | NIOS2_RA_REGNUM); | |
1452 | ||
1453 | /* Frame ID, since it's a frameless / stackless function, no stack | |
1454 | space is allocated and SP on entry is the current SP. */ | |
1455 | pc = get_frame_pc (this_frame); | |
1456 | find_pc_partial_function (pc, NULL, &start_addr, NULL); | |
1457 | stack_addr = get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM); | |
1458 | trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr)); | |
1459 | /* Assume that the frame's base is the same as the stack pointer. */ | |
1460 | trad_frame_set_this_base (this_trad_cache, stack_addr); | |
1461 | ||
1462 | return this_trad_cache; | |
1463 | } | |
1464 | ||
1465 | /* Implement the this_id function for the stub unwinder. */ | |
1466 | ||
1467 | static void | |
1468 | nios2_stub_frame_this_id (struct frame_info *this_frame, void **this_cache, | |
1469 | struct frame_id *this_id) | |
1470 | { | |
1471 | struct trad_frame_cache *this_trad_cache | |
1472 | = nios2_stub_frame_cache (this_frame, this_cache); | |
1473 | ||
1474 | trad_frame_get_id (this_trad_cache, this_id); | |
1475 | } | |
1476 | ||
1477 | /* Implement the prev_register function for the stub unwinder. */ | |
1478 | ||
1479 | static struct value * | |
1480 | nios2_stub_frame_prev_register (struct frame_info *this_frame, | |
1481 | void **this_cache, int regnum) | |
1482 | { | |
1483 | struct trad_frame_cache *this_trad_cache | |
1484 | = nios2_stub_frame_cache (this_frame, this_cache); | |
1485 | ||
1486 | return trad_frame_get_register (this_trad_cache, this_frame, regnum); | |
1487 | } | |
1488 | ||
1489 | /* Implement the sniffer function for the stub unwinder. | |
1490 | This unwinder is used for cases where the normal | |
1491 | prologue-analysis-based unwinder can't work, | |
1492 | such as PLT stubs. */ | |
1493 | ||
1494 | static int | |
1495 | nios2_stub_frame_sniffer (const struct frame_unwind *self, | |
1496 | struct frame_info *this_frame, void **cache) | |
1497 | { | |
1498 | gdb_byte dummy[4]; | |
1499 | struct obj_section *s; | |
1500 | CORE_ADDR pc = get_frame_address_in_block (this_frame); | |
1501 | ||
1502 | /* Use the stub unwinder for unreadable code. */ | |
1503 | if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0) | |
1504 | return 1; | |
1505 | ||
3e5d3a5a | 1506 | if (in_plt_section (pc)) |
a1217d97 SL |
1507 | return 1; |
1508 | ||
1509 | return 0; | |
1510 | } | |
1511 | ||
a1217d97 SL |
1512 | /* Define the data structures for the stub unwinder. */ |
1513 | ||
1514 | static const struct frame_unwind nios2_stub_frame_unwind = | |
1515 | { | |
1516 | NORMAL_FRAME, | |
1517 | default_frame_unwind_stop_reason, | |
1518 | nios2_stub_frame_this_id, | |
1519 | nios2_stub_frame_prev_register, | |
1520 | NULL, | |
1521 | nios2_stub_frame_sniffer | |
1522 | }; | |
1523 | ||
a1217d97 | 1524 | |
a1217d97 SL |
1525 | |
1526 | /* Determine where to set a single step breakpoint while considering | |
1527 | branch prediction. */ | |
1528 | ||
1529 | static CORE_ADDR | |
1530 | nios2_get_next_pc (struct frame_info *frame, CORE_ADDR pc) | |
1531 | { | |
1532 | struct gdbarch *gdbarch = get_frame_arch (frame); | |
1533 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
d53c26c7 SL |
1534 | unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach; |
1535 | unsigned int insn; | |
1536 | const struct nios2_opcode *op = nios2_fetch_insn (gdbarch, pc, &insn); | |
a1217d97 SL |
1537 | int ra; |
1538 | int rb; | |
d53c26c7 SL |
1539 | int imm; |
1540 | unsigned int uimm; | |
1541 | int wb, ret; | |
1542 | enum branch_condition cond; | |
1543 | ||
1544 | /* Do something stupid if we can't disassemble the insn at pc. */ | |
1545 | if (op == NULL) | |
1546 | return pc + NIOS2_OPCODE_SIZE; | |
1547 | ||
1548 | if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)) | |
a1217d97 | 1549 | { |
d53c26c7 SL |
1550 | int ras = get_frame_register_signed (frame, ra); |
1551 | int rbs = get_frame_register_signed (frame, rb); | |
1552 | unsigned int rau = get_frame_register_unsigned (frame, ra); | |
1553 | unsigned int rbu = get_frame_register_unsigned (frame, rb); | |
a1217d97 | 1554 | |
d53c26c7 SL |
1555 | pc += op->size; |
1556 | switch (cond) | |
a1217d97 | 1557 | { |
d53c26c7 SL |
1558 | case branch_none: |
1559 | pc += imm; | |
1560 | break; | |
1561 | case branch_eq: | |
1562 | if (ras == rbs) | |
1563 | pc += imm; | |
1564 | break; | |
1565 | case branch_ne: | |
1566 | if (ras != rbs) | |
1567 | pc += imm; | |
1568 | break; | |
1569 | case branch_ge: | |
1570 | if (ras >= rbs) | |
1571 | pc += imm; | |
1572 | break; | |
1573 | case branch_geu: | |
1574 | if (rau >= rbu) | |
1575 | pc += imm; | |
1576 | break; | |
1577 | case branch_lt: | |
1578 | if (ras < rbs) | |
1579 | pc += imm; | |
1580 | break; | |
1581 | case branch_ltu: | |
1582 | if (rau < rbu) | |
1583 | pc += imm; | |
a1217d97 | 1584 | break; |
a1217d97 SL |
1585 | default: |
1586 | break; | |
1587 | } | |
a1217d97 | 1588 | } |
d53c26c7 SL |
1589 | |
1590 | else if (nios2_match_jmpi (insn, op, mach, &uimm) | |
1591 | || nios2_match_calli (insn, op, mach, &uimm)) | |
1592 | pc = (pc & 0xf0000000) | uimm; | |
1593 | ||
1594 | else if (nios2_match_jmpr (insn, op, mach, &ra) | |
1595 | || nios2_match_callr (insn, op, mach, &ra)) | |
1596 | pc = get_frame_register_unsigned (frame, ra); | |
1597 | ||
1598 | else if (nios2_match_trap (insn, op, mach, &uimm)) | |
1599 | { | |
1600 | if (tdep->syscall_next_pc != NULL) | |
1601 | return tdep->syscall_next_pc (frame); | |
1602 | } | |
1603 | ||
1604 | else | |
1605 | pc += op->size; | |
1606 | ||
a1217d97 SL |
1607 | return pc; |
1608 | } | |
1609 | ||
1610 | /* Implement the software_single_step gdbarch method. */ | |
1611 | ||
1612 | static int | |
1613 | nios2_software_single_step (struct frame_info *frame) | |
1614 | { | |
1615 | struct gdbarch *gdbarch = get_frame_arch (frame); | |
1616 | struct address_space *aspace = get_frame_address_space (frame); | |
1617 | CORE_ADDR next_pc = nios2_get_next_pc (frame, get_frame_pc (frame)); | |
1618 | ||
1619 | insert_single_step_breakpoint (gdbarch, aspace, next_pc); | |
1620 | ||
1621 | return 1; | |
1622 | } | |
1623 | ||
1624 | /* Implement the get_longjump_target gdbarch method. */ | |
1625 | ||
1626 | static int | |
1627 | nios2_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc) | |
1628 | { | |
1629 | struct gdbarch *gdbarch = get_frame_arch (frame); | |
1630 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
1631 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
1632 | CORE_ADDR jb_addr = get_frame_register_unsigned (frame, NIOS2_R4_REGNUM); | |
1633 | gdb_byte buf[4]; | |
1634 | ||
1635 | if (target_read_memory (jb_addr + (tdep->jb_pc * 4), buf, 4)) | |
1636 | return 0; | |
1637 | ||
1638 | *pc = extract_unsigned_integer (buf, 4, byte_order); | |
1639 | return 1; | |
1640 | } | |
1641 | ||
1642 | /* Initialize the Nios II gdbarch. */ | |
1643 | ||
1644 | static struct gdbarch * | |
1645 | nios2_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) | |
1646 | { | |
1647 | struct gdbarch *gdbarch; | |
1648 | struct gdbarch_tdep *tdep; | |
1649 | int register_bytes, i; | |
1650 | struct tdesc_arch_data *tdesc_data = NULL; | |
1651 | const struct target_desc *tdesc = info.target_desc; | |
1652 | ||
1653 | if (!tdesc_has_registers (tdesc)) | |
1654 | /* Pick a default target description. */ | |
1655 | tdesc = tdesc_nios2; | |
1656 | ||
1657 | /* Check any target description for validity. */ | |
1658 | if (tdesc_has_registers (tdesc)) | |
1659 | { | |
1660 | const struct tdesc_feature *feature; | |
1661 | int valid_p; | |
1662 | ||
1663 | feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nios2.cpu"); | |
1664 | if (feature == NULL) | |
1665 | return NULL; | |
1666 | ||
1667 | tdesc_data = tdesc_data_alloc (); | |
1668 | ||
1669 | valid_p = 1; | |
1670 | ||
1671 | for (i = 0; i < NIOS2_NUM_REGS; i++) | |
1672 | valid_p &= tdesc_numbered_register (feature, tdesc_data, i, | |
1673 | nios2_reg_names[i]); | |
1674 | ||
1675 | if (!valid_p) | |
1676 | { | |
1677 | tdesc_data_cleanup (tdesc_data); | |
1678 | return NULL; | |
1679 | } | |
1680 | } | |
1681 | ||
1682 | /* Find a candidate among the list of pre-declared architectures. */ | |
1683 | arches = gdbarch_list_lookup_by_info (arches, &info); | |
1684 | if (arches != NULL) | |
1685 | return arches->gdbarch; | |
1686 | ||
1687 | /* None found, create a new architecture from the information | |
1688 | provided. */ | |
1689 | tdep = xcalloc (1, sizeof (struct gdbarch_tdep)); | |
1690 | gdbarch = gdbarch_alloc (&info, tdep); | |
1691 | ||
1692 | /* longjmp support not enabled by default. */ | |
1693 | tdep->jb_pc = -1; | |
1694 | ||
1695 | /* Data type sizes. */ | |
1696 | set_gdbarch_ptr_bit (gdbarch, 32); | |
1697 | set_gdbarch_addr_bit (gdbarch, 32); | |
1698 | set_gdbarch_short_bit (gdbarch, 16); | |
1699 | set_gdbarch_int_bit (gdbarch, 32); | |
1700 | set_gdbarch_long_bit (gdbarch, 32); | |
1701 | set_gdbarch_long_long_bit (gdbarch, 64); | |
1702 | set_gdbarch_float_bit (gdbarch, 32); | |
1703 | set_gdbarch_double_bit (gdbarch, 64); | |
1704 | ||
1705 | set_gdbarch_float_format (gdbarch, floatformats_ieee_single); | |
1706 | set_gdbarch_double_format (gdbarch, floatformats_ieee_double); | |
1707 | ||
1708 | /* The register set. */ | |
1709 | set_gdbarch_num_regs (gdbarch, NIOS2_NUM_REGS); | |
1710 | set_gdbarch_sp_regnum (gdbarch, NIOS2_SP_REGNUM); | |
1711 | set_gdbarch_pc_regnum (gdbarch, NIOS2_PC_REGNUM); /* Pseudo register PC */ | |
1712 | ||
1713 | set_gdbarch_register_name (gdbarch, nios2_register_name); | |
1714 | set_gdbarch_register_type (gdbarch, nios2_register_type); | |
1715 | ||
1716 | /* Provide register mappings for stabs and dwarf2. */ | |
1717 | set_gdbarch_stab_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum); | |
1718 | set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum); | |
1719 | ||
1720 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); | |
1721 | ||
1722 | /* Call dummy code. */ | |
1723 | set_gdbarch_frame_align (gdbarch, nios2_frame_align); | |
1724 | ||
1725 | set_gdbarch_return_value (gdbarch, nios2_return_value); | |
1726 | ||
1727 | set_gdbarch_skip_prologue (gdbarch, nios2_skip_prologue); | |
1728 | set_gdbarch_in_function_epilogue_p (gdbarch, nios2_in_function_epilogue_p); | |
1729 | set_gdbarch_breakpoint_from_pc (gdbarch, nios2_breakpoint_from_pc); | |
1730 | ||
1731 | set_gdbarch_dummy_id (gdbarch, nios2_dummy_id); | |
1732 | set_gdbarch_unwind_pc (gdbarch, nios2_unwind_pc); | |
1733 | set_gdbarch_unwind_sp (gdbarch, nios2_unwind_sp); | |
1734 | ||
1735 | /* The dwarf2 unwinder will normally produce the best results if | |
1736 | the debug information is available, so register it first. */ | |
1737 | dwarf2_append_unwinders (gdbarch); | |
1738 | frame_unwind_append_unwinder (gdbarch, &nios2_stub_frame_unwind); | |
1739 | frame_unwind_append_unwinder (gdbarch, &nios2_frame_unwind); | |
1740 | ||
1741 | /* Single stepping. */ | |
1742 | set_gdbarch_software_single_step (gdbarch, nios2_software_single_step); | |
1743 | ||
1744 | /* Hook in ABI-specific overrides, if they have been registered. */ | |
1745 | gdbarch_init_osabi (info, gdbarch); | |
1746 | ||
1747 | if (tdep->jb_pc >= 0) | |
1748 | set_gdbarch_get_longjmp_target (gdbarch, nios2_get_longjmp_target); | |
1749 | ||
1750 | frame_base_set_default (gdbarch, &nios2_frame_base); | |
1751 | ||
1752 | set_gdbarch_print_insn (gdbarch, nios2_print_insn); | |
1753 | ||
1754 | /* Enable inferior call support. */ | |
1755 | set_gdbarch_push_dummy_call (gdbarch, nios2_push_dummy_call); | |
1756 | ||
1757 | if (tdesc_data) | |
1758 | tdesc_use_registers (gdbarch, tdesc, tdesc_data); | |
1759 | ||
1760 | return gdbarch; | |
1761 | } | |
1762 | ||
1763 | extern initialize_file_ftype _initialize_nios2_tdep; /* -Wmissing-prototypes */ | |
1764 | ||
1765 | void | |
1766 | _initialize_nios2_tdep (void) | |
1767 | { | |
1768 | gdbarch_register (bfd_arch_nios2, nios2_gdbarch_init, NULL); | |
1769 | initialize_tdesc_nios2 (); | |
1770 | ||
1771 | /* Allow debugging this file's internals. */ | |
1772 | add_setshow_boolean_cmd ("nios2", class_maintenance, &nios2_debug, | |
1773 | _("Set Nios II debugging."), | |
1774 | _("Show Nios II debugging."), | |
1775 | _("When on, Nios II specific debugging is enabled."), | |
1776 | NULL, | |
1777 | NULL, | |
1778 | &setdebuglist, &showdebuglist); | |
1779 | } |