1 /* Target-dependent code for the CSKY architecture, for GDB.
3 Copyright (C) 2010-2019 Free Software Foundation, Inc.
5 Contributed by C-SKY Microsystems and Mentor Graphics.
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 /* Standard C++ includes. */
28 /* Local non-gdb includes. */
29 #include "arch-utils.h"
31 #include "common/gdb_assert.h"
32 #include "csky-tdep.h"
34 #include "dwarf2-frame.h"
37 #include "floatformat.h"
38 #include "frame-base.h"
39 #include "frame-unwind.h"
48 #include "opcode/csky.h"
51 #include "reggroups.h"
54 #include "sim-regno.h"
58 #include "target-descriptions.h"
60 #include "trad-frame.h"
61 #include "user-regs.h"
65 /* Control debugging information emitted in this file. */
66 static int csky_debug
= 0;
68 static struct reggroup
*cr_reggroup
;
69 static struct reggroup
*fr_reggroup
;
70 static struct reggroup
*vr_reggroup
;
71 static struct reggroup
*mmu_reggroup
;
72 static struct reggroup
*prof_reggroup
;
74 /* Convenience function to print debug messages in prologue analysis. */
77 print_savedreg_msg (int regno
, int offsets
[], bool print_continuing
)
79 fprintf_unfiltered (gdb_stdlog
, "csky: r%d saved at offset 0x%x\n",
80 regno
, offsets
[regno
]);
82 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
85 /* Check whether the instruction at ADDR is 16-bit or not. */
88 csky_pc_is_csky16 (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
90 gdb_byte target_mem
[2];
94 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
96 status
= target_read_memory (addr
, target_mem
, 2);
97 /* Assume a 16-bit instruction if we can't read memory. */
101 /* Get instruction from memory. */
102 insn
= extract_unsigned_integer (target_mem
, 2, byte_order
);
103 if ((insn
& CSKY_32_INSN_MASK
) == CSKY_32_INSN_MASK
)
105 else if (insn
== CSKY_BKPT_INSN
)
107 /* Check for 32-bit bkpt instruction which is all 0. */
108 status
= target_read_memory (addr
+ 2, target_mem
, 2);
112 insn
= extract_unsigned_integer (target_mem
, 2, byte_order
);
113 if (insn
== CSKY_BKPT_INSN
)
119 /* Get one instruction at ADDR and store it in INSN. Return 2 for
120 a 16-bit instruction or 4 for a 32-bit instruction. */
123 csky_get_insn (struct gdbarch
*gdbarch
, CORE_ADDR addr
, unsigned int *insn
)
125 gdb_byte target_mem
[2];
126 unsigned int insn_type
;
129 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
131 status
= target_read_memory (addr
, target_mem
, 2);
133 memory_error (TARGET_XFER_E_IO
, addr
);
135 insn_type
= extract_unsigned_integer (target_mem
, 2, byte_order
);
136 if (CSKY_32_INSN_MASK
== (insn_type
& CSKY_32_INSN_MASK
))
138 status
= target_read_memory (addr
+ 2, target_mem
, 2);
140 memory_error (TARGET_XFER_E_IO
, addr
);
141 insn_type
= ((insn_type
<< 16)
142 | extract_unsigned_integer (target_mem
, 2, byte_order
));
149 /* Implement the read_pc gdbarch method. */
152 csky_read_pc (readable_regcache
*regcache
)
155 regcache
->cooked_read (CSKY_PC_REGNUM
, &pc
);
159 /* Implement the write_pc gdbarch method. */
162 csky_write_pc (regcache
*regcache
, CORE_ADDR val
)
164 regcache_cooked_write_unsigned (regcache
, CSKY_PC_REGNUM
, val
);
167 /* C-Sky ABI register names. */
169 static const char *csky_register_names
[] =
171 /* General registers 0 - 31. */
172 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
173 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
174 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
175 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
177 /* DSP hilo registers 36 and 37. */
178 "", "", "", "", "hi", "lo", "", "",
180 /* FPU/VPU general registers 40 - 71. */
181 "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
182 "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
183 "vr0", "vr1", "vr2", "vr3", "vr4", "vr5", "vr6", "vr7",
184 "vr8", "vr9", "vr10", "vr11", "vr12", "vr13", "vr14", "vr15",
186 /* Program counter 72. */
189 /* Optional registers (ar) 73 - 88. */
190 "ar0", "ar1", "ar2", "ar3", "ar4", "ar5", "ar6", "ar7",
191 "ar8", "ar9", "ar10", "ar11", "ar12", "ar13", "ar14", "ar15",
193 /* Control registers (cr) 89 - 119. */
194 "psr", "vbr", "epsr", "fpsr", "epc", "fpc", "ss0", "ss1",
195 "ss2", "ss3", "ss4", "gcr", "gsr", "cr13", "cr14", "cr15",
196 "cr16", "cr17", "cr18", "cr19", "cr20", "cr21", "cr22", "cr23",
197 "cr24", "cr25", "cr26", "cr27", "cr28", "cr29", "cr30", "cr31",
199 /* FPU/VPU control registers 121 ~ 123. */
201 "fid", "fcr", "fesr", "", "", "", "usp",
203 /* MMU control registers: 128 - 136. */
204 "mcr0", "mcr2", "mcr3", "mcr4", "mcr6", "mcr8", "mcr29", "mcr30",
207 /* Profiling control registers 140 - 143. */
208 /* Profiling software general registers 144 - 157. */
209 "profcr0", "profcr1", "profcr2", "profcr3", "profsgr0", "profsgr1",
210 "profsgr2", "profsgr3", "profsgr4", "profsgr5", "profsgr6", "profsgr7",
211 "profsgr8", "profsgr9", "profsgr10","profsgr11","profsgr12", "profsgr13",
214 /* Profiling architecture general registers 160 - 174. */
215 "profagr0", "profagr1", "profagr2", "profagr3", "profagr4", "profagr5",
216 "profagr6", "profagr7", "profagr8", "profagr9", "profagr10","profagr11",
217 "profagr12","profagr13","profagr14", "",
219 /* Profiling extension general registers 176 - 188. */
220 "profxgr0", "profxgr1", "profxgr2", "profxgr3", "profxgr4", "profxgr5",
221 "profxgr6", "profxgr7", "profxgr8", "profxgr9", "profxgr10","profxgr11",
224 /* Control registers in bank1. */
225 "", "", "", "", "", "", "", "",
226 "", "", "", "", "", "", "", "",
227 "cp1cr16", "cp1cr17", "cp1cr18", "cp1cr19", "cp1cr20", "", "", "",
228 "", "", "", "", "", "", "", "",
230 /* Control registers in bank3 (ICE). */
231 "sepsr", "sevbr", "seepsr", "", "seepc", "", "nsssp", "seusp",
232 "sedcr", "", "", "", "", "", "", "",
233 "", "", "", "", "", "", "", "",
234 "", "", "", "", "", "", "", ""
237 /* Implement the register_name gdbarch method. */
240 csky_register_name (struct gdbarch
*gdbarch
, int reg_nr
)
242 if (tdesc_has_registers (gdbarch_target_desc (gdbarch
)))
243 return tdesc_register_name (gdbarch
, reg_nr
);
248 if (reg_nr
>= gdbarch_num_regs (gdbarch
))
251 return csky_register_names
[reg_nr
];
254 /* Construct vector type for vrx registers. */
257 csky_vector_type (struct gdbarch
*gdbarch
)
259 const struct builtin_type
*bt
= builtin_type (gdbarch
);
263 t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vec128i",
266 append_composite_type_field (t
, "u32",
267 init_vector_type (bt
->builtin_int32
, 4));
268 append_composite_type_field (t
, "u16",
269 init_vector_type (bt
->builtin_int16
, 8));
270 append_composite_type_field (t
, "u8",
271 init_vector_type (bt
->builtin_int8
, 16));
274 TYPE_NAME (t
) = "builtin_type_vec128i";
279 /* Return the GDB type object for the "standard" data type
280 of data in register N. */
283 csky_register_type (struct gdbarch
*gdbarch
, int reg_nr
)
285 /* PC, EPC, FPC is a text pointer. */
286 if ((reg_nr
== CSKY_PC_REGNUM
) || (reg_nr
== CSKY_EPC_REGNUM
)
287 || (reg_nr
== CSKY_FPC_REGNUM
))
288 return builtin_type (gdbarch
)->builtin_func_ptr
;
290 /* VBR is a data pointer. */
291 if (reg_nr
== CSKY_VBR_REGNUM
)
292 return builtin_type (gdbarch
)->builtin_data_ptr
;
294 /* Float register has 64 bits, and only in ck810. */
295 if ((reg_nr
>=CSKY_FR0_REGNUM
) && (reg_nr
<= CSKY_FR0_REGNUM
+ 15))
296 return arch_float_type (gdbarch
, 64, "builtin_type_csky_ext",
297 floatformats_ieee_double
);
299 /* Vector register has 128 bits, and only in ck810. */
300 if ((reg_nr
>= CSKY_VR0_REGNUM
) && (reg_nr
<= CSKY_VR0_REGNUM
+ 15))
301 return csky_vector_type (gdbarch
);
303 /* Profiling general register has 48 bits, we use 64bit. */
304 if ((reg_nr
>= CSKY_PROFGR_REGNUM
) && (reg_nr
<= CSKY_PROFGR_REGNUM
+ 44))
305 return builtin_type (gdbarch
)->builtin_uint64
;
307 if (reg_nr
== CSKY_SP_REGNUM
)
308 return builtin_type (gdbarch
)->builtin_data_ptr
;
310 /* Others are 32 bits. */
311 return builtin_type (gdbarch
)->builtin_int32
;
314 /* Data structure to marshall items in a dummy stack frame when
315 calling a function in the inferior. */
319 stack_item (int len_
, const gdb_byte
*data_
)
320 : len (len_
), data (data_
)
324 const gdb_byte
*data
;
327 /* Implement the push_dummy_call gdbarch method. */
330 csky_push_dummy_call (struct gdbarch
*gdbarch
, struct value
*function
,
331 struct regcache
*regcache
, CORE_ADDR bp_addr
,
332 int nargs
, struct value
**args
, CORE_ADDR sp
,
333 function_call_return_method return_method
,
334 CORE_ADDR struct_addr
)
337 int argreg
= CSKY_ABI_A0_REGNUM
;
338 int last_arg_regnum
= CSKY_ABI_LAST_ARG_REGNUM
;
339 int need_dummy_stack
= 0;
340 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
341 std::vector
<stack_item
> stack_items
;
343 /* Set the return address. For CSKY, the return breakpoint is
344 always at BP_ADDR. */
345 regcache_cooked_write_unsigned (regcache
, CSKY_LR_REGNUM
, bp_addr
);
347 /* The struct_return pointer occupies the first parameter
349 if (return_method
== return_method_struct
)
353 fprintf_unfiltered (gdb_stdlog
,
354 "csky: struct return in %s = %s\n",
355 gdbarch_register_name (gdbarch
, argreg
),
356 paddress (gdbarch
, struct_addr
));
358 regcache_cooked_write_unsigned (regcache
, argreg
, struct_addr
);
362 /* Put parameters into argument registers in REGCACHE.
363 In ABI argument registers are r0 through r3. */
364 for (argnum
= 0; argnum
< nargs
; argnum
++)
367 struct type
*arg_type
;
370 arg_type
= check_typedef (value_type (args
[argnum
]));
371 len
= TYPE_LENGTH (arg_type
);
372 val
= value_contents (args
[argnum
]);
374 /* Copy the argument to argument registers or the dummy stack.
375 Large arguments are split between registers and stack.
377 If len < 4, there is no need to worry about endianness since
378 the arguments will always be stored in the low address. */
382 = extract_unsigned_integer (val
, len
, byte_order
);
383 regcache_cooked_write_unsigned (regcache
, argreg
, regval
);
390 int partial_len
= len
< 4 ? len
: 4;
391 if (argreg
<= last_arg_regnum
)
393 /* The argument is passed in an argument register. */
395 = extract_unsigned_integer (val
, partial_len
,
397 if (byte_order
== BFD_ENDIAN_BIG
)
398 regval
<<= (4 - partial_len
) * 8;
400 /* Put regval into register in REGCACHE. */
401 regcache_cooked_write_unsigned (regcache
, argreg
,
407 /* The argument should be pushed onto the dummy stack. */
408 stack_items
.emplace_back (4, val
);
409 need_dummy_stack
+= 4;
417 /* Transfer the dummy stack frame to the target. */
418 std::vector
<stack_item
>::reverse_iterator iter
;
419 for (iter
= stack_items
.rbegin (); iter
!= stack_items
.rend (); ++iter
)
422 write_memory (sp
, iter
->data
, iter
->len
);
425 /* Finally, update the SP register. */
426 regcache_cooked_write_unsigned (regcache
, CSKY_SP_REGNUM
, sp
);
430 /* Implement the return_value gdbarch method. */
432 static enum return_value_convention
433 csky_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
434 struct type
*valtype
, struct regcache
*regcache
,
435 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
438 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
439 int len
= TYPE_LENGTH (valtype
);
440 unsigned int ret_regnum
= CSKY_RET_REGNUM
;
442 /* Csky abi specifies that return values larger than 8 bytes
443 are put on the stack. */
445 return RETURN_VALUE_STRUCT_CONVENTION
;
451 /* By using store_unsigned_integer we avoid having to do
452 anything special for small big-endian values. */
453 regcache
->cooked_read (ret_regnum
, &tmp
);
454 store_unsigned_integer (readbuf
, (len
> 4 ? 4 : len
),
458 regcache
->cooked_read (ret_regnum
+ 1, &tmp
);
459 store_unsigned_integer (readbuf
+ 4, 4, byte_order
, tmp
);
462 if (writebuf
!= NULL
)
464 regval
= extract_unsigned_integer (writebuf
, len
> 4 ? 4 : len
,
466 regcache_cooked_write_unsigned (regcache
, ret_regnum
, regval
);
469 regval
= extract_unsigned_integer ((gdb_byte
*) writebuf
+ 4,
471 regcache_cooked_write_unsigned (regcache
, ret_regnum
+ 1,
476 return RETURN_VALUE_REGISTER_CONVENTION
;
480 /* Implement the frame_align gdbarch method.
482 Adjust the address downward (direction of stack growth) so that it
483 is correctly aligned for a new stack frame. */
486 csky_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
488 return align_down (addr
, 4);
491 /* Unwind cache used for gdbarch fallback unwinder. */
493 struct csky_unwind_cache
495 /* The stack pointer at the time this frame was created; i.e. the
496 caller's stack pointer when this function was called. It is used
497 to identify this frame. */
500 /* The frame base for this frame is just prev_sp - frame size.
501 FRAMESIZE is the distance from the frame pointer to the
502 initial stack pointer. */
505 /* The register used to hold the frame pointer for this frame. */
508 /* Saved register offsets. */
509 struct trad_frame_saved_reg
*saved_regs
;
512 /* Do prologue analysis, returning the PC of the first instruction
513 after the function prologue. */
516 csky_analyze_prologue (struct gdbarch
*gdbarch
,
520 struct frame_info
*this_frame
,
521 struct csky_unwind_cache
*this_cache
,
525 unsigned int insn
, rn
;
528 int register_offsets
[CSKY_NUM_GREGS_SAVED_GREGS
];
530 /* For adjusting fp. */
534 /* REGISTER_OFFSETS will contain offsets from the top of the frame
535 (NOT the frame pointer) for the various saved registers, or -1
536 if the register is not saved. */
537 for (rn
= 0; rn
< CSKY_NUM_GREGS_SAVED_GREGS
; rn
++)
538 register_offsets
[rn
] = -1;
540 /* Analyze the prologue. Things we determine from analyzing the
541 prologue include the size of the frame and which registers are
542 saved (and where). */
545 fprintf_unfiltered (gdb_stdlog
,
546 "csky: Scanning prologue: start_pc = 0x%x,"
547 "limit_pc = 0x%x\n", (unsigned int) start_pc
,
548 (unsigned int) limit_pc
);
551 /* Default to 16 bit instruction. */
554 for (addr
= start_pc
; addr
< limit_pc
; addr
+= insn_len
)
557 insn_len
= csky_get_insn (gdbarch
, addr
, &insn
);
559 /* Check if 32 bit. */
562 /* subi32 sp,sp oimm12. */
563 if (CSKY_32_IS_SUBI0 (insn
))
566 int offset
= CSKY_32_SUBI_IMM (insn
);
569 fprintf_unfiltered (gdb_stdlog
,
570 "csky: got subi sp,%d; continuing\n",
576 /* stm32 ry-rz,(sp). */
577 else if (CSKY_32_IS_STMx0 (insn
))
579 /* Spill register(s). */
584 /* BIG WARNING! The CKCore ABI does not restrict functions
585 to taking only one stack allocation. Therefore, when
586 we save a register, we record the offset of where it was
587 saved relative to the current stacksize. This will
588 then give an offset from the SP upon entry to our
589 function. Remember, stacksize is NOT constant until
590 we're done scanning the prologue. */
591 start_register
= CSKY_32_STM_VAL_REGNUM (insn
);
592 reg_count
= CSKY_32_STM_SIZE (insn
);
595 fprintf_unfiltered (gdb_stdlog
,
596 "csky: got stm r%d-r%d,(sp)\n",
598 start_register
+ reg_count
);
601 for (rn
= start_register
, offset
= 0;
602 rn
<= start_register
+ reg_count
;
605 register_offsets
[rn
] = stacksize
- offset
;
608 fprintf_unfiltered (gdb_stdlog
,
609 "csky: r%d saved at 0x%x"
611 rn
, register_offsets
[rn
],
616 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
619 /* stw ry,(sp,disp). */
620 else if (CSKY_32_IS_STWx0 (insn
))
622 /* Spill register: see note for IS_STM above. */
625 rn
= CSKY_32_ST_VAL_REGNUM (insn
);
626 disp
= CSKY_32_ST_OFFSET (insn
);
627 register_offsets
[rn
] = stacksize
- disp
;
629 print_savedreg_msg (rn
, register_offsets
, true);
632 else if (CSKY_32_IS_MOV_FP_SP (insn
))
634 /* SP is saved to FP reg, means code afer prologue may
637 adjust_fp
= stacksize
;
640 else if (CSKY_32_IS_MFCR_EPSR (insn
))
644 int mfcr_regnum
= insn
& 0x1f;
645 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
648 int stw_regnum
= (insn2
>> 5) & 0x7;
649 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
653 /* CSKY_EPSR_REGNUM. */
655 offset
= CSKY_16_STWx0_OFFSET (insn2
);
656 register_offsets
[rn
] = stacksize
- offset
;
658 print_savedreg_msg (rn
, register_offsets
, true);
666 int stw_regnum
= (insn2
>> 21) & 0x1f;
667 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
671 /* CSKY_EPSR_REGNUM. */
673 offset
= CSKY_32_ST_OFFSET (insn2
);
674 register_offsets
[rn
] = framesize
- offset
;
676 print_savedreg_msg (rn
, register_offsets
, true);
682 else if (CSKY_32_IS_MFCR_FPSR (insn
))
686 int mfcr_regnum
= insn
& 0x1f;
687 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
690 int stw_regnum
= (insn2
>> 5) & 0x7;
691 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
696 /* CSKY_FPSR_REGNUM. */
697 rn
= CSKY_NUM_GREGS
+ 1;
698 offset
= CSKY_16_STWx0_OFFSET (insn2
);
699 register_offsets
[rn
] = stacksize
- offset
;
701 print_savedreg_msg (rn
, register_offsets
, true);
709 int stw_regnum
= (insn2
>> 21) & 0x1f;
710 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
714 /* CSKY_FPSR_REGNUM. */
715 rn
= CSKY_NUM_GREGS
+ 1;
716 offset
= CSKY_32_ST_OFFSET (insn2
);
717 register_offsets
[rn
] = framesize
- offset
;
719 print_savedreg_msg (rn
, register_offsets
, true);
725 else if (CSKY_32_IS_MFCR_EPC (insn
))
729 int mfcr_regnum
= insn
& 0x1f;
730 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
733 int stw_regnum
= (insn2
>> 5) & 0x7;
734 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
738 /* CSKY_EPC_REGNUM. */
739 rn
= CSKY_NUM_GREGS
+ 2;
740 offset
= CSKY_16_STWx0_OFFSET (insn2
);
741 register_offsets
[rn
] = stacksize
- offset
;
743 print_savedreg_msg (rn
, register_offsets
, true);
751 int stw_regnum
= (insn2
>> 21) & 0x1f;
752 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
756 /* CSKY_EPC_REGNUM. */
757 rn
= CSKY_NUM_GREGS
+ 2;
758 offset
= CSKY_32_ST_OFFSET (insn2
);
759 register_offsets
[rn
] = framesize
- offset
;
761 print_savedreg_msg (rn
, register_offsets
, true);
767 else if (CSKY_32_IS_MFCR_FPC (insn
))
771 int mfcr_regnum
= insn
& 0x1f;
772 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
775 int stw_regnum
= (insn2
>> 5) & 0x7;
776 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
780 /* CSKY_FPC_REGNUM. */
781 rn
= CSKY_NUM_GREGS
+ 3;
782 offset
= CSKY_16_STWx0_OFFSET (insn2
);
783 register_offsets
[rn
] = stacksize
- offset
;
785 print_savedreg_msg (rn
, register_offsets
, true);
793 int stw_regnum
= (insn2
>> 21) & 0x1f;
794 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
798 /* CSKY_FPC_REGNUM. */
799 rn
= CSKY_NUM_GREGS
+ 3;
800 offset
= CSKY_32_ST_OFFSET (insn2
);
801 register_offsets
[rn
] = framesize
- offset
;
803 print_savedreg_msg (rn
, register_offsets
, true);
809 else if (CSKY_32_IS_PUSH (insn
))
811 /* Push for 32_bit. */
813 if (CSKY_32_IS_PUSH_R29 (insn
))
816 register_offsets
[29] = stacksize
;
818 print_savedreg_msg (29, register_offsets
, false);
821 if (CSKY_32_PUSH_LIST2 (insn
))
823 int num
= CSKY_32_PUSH_LIST2 (insn
);
825 stacksize
+= num
* 4;
829 fprintf_unfiltered (gdb_stdlog
,
830 "csky: push regs_array: r16-r%d\n",
833 for (rn
= 16; rn
<= 16 + num
- 1; rn
++)
835 register_offsets
[rn
] = stacksize
- tmp
;
838 fprintf_unfiltered (gdb_stdlog
,
839 "csky: r%d saved at 0x%x"
840 " (offset %d)\n", rn
,
841 register_offsets
[rn
], tmp
);
846 if (CSKY_32_IS_PUSH_R15 (insn
))
849 register_offsets
[15] = stacksize
;
851 print_savedreg_msg (15, register_offsets
, false);
854 if (CSKY_32_PUSH_LIST1 (insn
))
856 int num
= CSKY_32_PUSH_LIST1 (insn
);
858 stacksize
+= num
* 4;
862 fprintf_unfiltered (gdb_stdlog
,
863 "csky: push regs_array: r4-r%d\n",
866 for (rn
= 4; rn
<= 4 + num
- 1; rn
++)
868 register_offsets
[rn
] = stacksize
- tmp
;
871 fprintf_unfiltered (gdb_stdlog
,
872 "csky: r%d saved at 0x%x"
873 " (offset %d)\n", rn
,
874 register_offsets
[rn
], tmp
);
880 framesize
= stacksize
;
882 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
885 else if (CSKY_32_IS_LRW4 (insn
) || CSKY_32_IS_MOVI4 (insn
)
886 || CSKY_32_IS_MOVIH4 (insn
) || CSKY_32_IS_BMASKI4 (insn
))
894 fprintf_unfiltered (gdb_stdlog
,
895 "csky: looking at large frame\n");
897 if (CSKY_32_IS_LRW4 (insn
))
899 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
900 int literal_addr
= (addr
+ ((insn
& 0xffff) << 2))
902 adjust
= read_memory_unsigned_integer (literal_addr
, 4,
905 else if (CSKY_32_IS_MOVI4 (insn
))
906 adjust
= (insn
& 0xffff);
907 else if (CSKY_32_IS_MOVIH4 (insn
))
908 adjust
= (insn
& 0xffff) << 16;
911 /* CSKY_32_IS_BMASKI4 (insn). */
912 adjust
= (1 << (((insn
& 0x3e00000) >> 21) + 1)) - 1;
917 fprintf_unfiltered (gdb_stdlog
,
918 "csky: base stacksize=0x%x\n", adjust
);
920 /* May have zero or more insns which modify r4. */
921 fprintf_unfiltered (gdb_stdlog
,
922 "csky: looking for r4 adjusters...\n");
926 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
927 while (CSKY_IS_R4_ADJUSTER (insn2
))
929 if (CSKY_32_IS_ADDI4 (insn2
))
931 int imm
= (insn2
& 0xfff) + 1;
935 fprintf_unfiltered (gdb_stdlog
,
936 "csky: addi r4,%d\n", imm
);
939 else if (CSKY_32_IS_SUBI4 (insn2
))
941 int imm
= (insn2
& 0xfff) + 1;
945 fprintf_unfiltered (gdb_stdlog
,
946 "csky: subi r4,%d\n", imm
);
949 else if (CSKY_32_IS_NOR4 (insn2
))
954 fprintf_unfiltered (gdb_stdlog
,
955 "csky: nor r4,r4,r4\n");
958 else if (CSKY_32_IS_ROTLI4 (insn2
))
960 int imm
= ((insn2
>> 21) & 0x1f);
961 int temp
= adjust
>> (32 - imm
);
966 fprintf_unfiltered (gdb_stdlog
,
967 "csky: rotli r4,r4,%d\n", imm
);
970 else if (CSKY_32_IS_LISI4 (insn2
))
972 int imm
= ((insn2
>> 21) & 0x1f);
976 fprintf_unfiltered (gdb_stdlog
,
977 "csky: lsli r4,r4,%d\n", imm
);
980 else if (CSKY_32_IS_BSETI4 (insn2
))
982 int imm
= ((insn2
>> 21) & 0x1f);
983 adjust
|= (1 << imm
);
986 fprintf_unfiltered (gdb_stdlog
,
987 "csky: bseti r4,r4 %d\n", imm
);
990 else if (CSKY_32_IS_BCLRI4 (insn2
))
992 int imm
= ((insn2
>> 21) & 0x1f);
993 adjust
&= ~(1 << imm
);
996 fprintf_unfiltered (gdb_stdlog
,
997 "csky: bclri r4,r4 %d\n", imm
);
1000 else if (CSKY_32_IS_IXH4 (insn2
))
1005 fprintf_unfiltered (gdb_stdlog
,
1006 "csky: ixh r4,r4,r4\n");
1009 else if (CSKY_32_IS_IXW4 (insn2
))
1014 fprintf_unfiltered (gdb_stdlog
,
1015 "csky: ixw r4,r4,r4\n");
1018 else if (CSKY_16_IS_ADDI4 (insn2
))
1020 int imm
= (insn2
& 0xff) + 1;
1024 fprintf_unfiltered (gdb_stdlog
,
1025 "csky: addi r4,%d\n", imm
);
1028 else if (CSKY_16_IS_SUBI4 (insn2
))
1030 int imm
= (insn2
& 0xff) + 1;
1034 fprintf_unfiltered (gdb_stdlog
,
1035 "csky: subi r4,%d\n", imm
);
1038 else if (CSKY_16_IS_NOR4 (insn2
))
1043 fprintf_unfiltered (gdb_stdlog
,
1044 "csky: nor r4,r4\n");
1047 else if (CSKY_16_IS_BSETI4 (insn2
))
1049 int imm
= (insn2
& 0x1f);
1050 adjust
|= (1 << imm
);
1053 fprintf_unfiltered (gdb_stdlog
,
1054 "csky: bseti r4, %d\n", imm
);
1057 else if (CSKY_16_IS_BCLRI4 (insn2
))
1059 int imm
= (insn2
& 0x1f);
1060 adjust
&= ~(1 << imm
);
1063 fprintf_unfiltered (gdb_stdlog
,
1064 "csky: bclri r4, %d\n", imm
);
1067 else if (CSKY_16_IS_LSLI4 (insn2
))
1069 int imm
= (insn2
& 0x1f);
1073 fprintf_unfiltered (gdb_stdlog
,
1074 "csky: lsli r4,r4, %d\n", imm
);
1079 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
1084 fprintf_unfiltered (gdb_stdlog
, "csky: done looking for"
1088 /* If the next insn adjusts the stack pointer, we keep
1089 everything; if not, we scrap it and we've found the
1090 end of the prologue. */
1091 if (CSKY_IS_SUBU4 (insn2
))
1094 stacksize
+= adjust
;
1097 fprintf_unfiltered (gdb_stdlog
,
1098 "csky: found stack adjustment of"
1099 " 0x%x bytes.\n", adjust
);
1100 fprintf_unfiltered (gdb_stdlog
,
1101 "csky: skipping to new address %s\n",
1102 core_addr_to_string_nz (addr
));
1103 fprintf_unfiltered (gdb_stdlog
,
1104 "csky: continuing\n");
1109 /* None of these instructions are prologue, so don't touch
1113 fprintf_unfiltered (gdb_stdlog
,
1114 "csky: no subu sp,sp,r4; NOT altering"
1122 /* insn_len != 4. */
1124 /* subi.sp sp,disp. */
1125 if (CSKY_16_IS_SUBI0 (insn
))
1127 int offset
= CSKY_16_SUBI_IMM (insn
);
1130 fprintf_unfiltered (gdb_stdlog
,
1131 "csky: got subi r0,%d; continuing\n",
1134 stacksize
+= offset
;
1137 /* stw.16 rz,(sp,disp). */
1138 else if (CSKY_16_IS_STWx0 (insn
))
1140 /* Spill register: see note for IS_STM above. */
1143 rn
= CSKY_16_ST_VAL_REGNUM (insn
);
1144 disp
= CSKY_16_ST_OFFSET (insn
);
1145 register_offsets
[rn
] = stacksize
- disp
;
1147 print_savedreg_msg (rn
, register_offsets
, true);
1150 else if (CSKY_16_IS_MOV_FP_SP (insn
))
1152 /* SP is saved to FP reg, means prologue may modify SP. */
1154 adjust_fp
= stacksize
;
1157 else if (CSKY_16_IS_PUSH (insn
))
1159 /* Push for 16_bit. */
1161 if (CSKY_16_IS_PUSH_R15 (insn
))
1164 register_offsets
[15] = stacksize
;
1166 print_savedreg_msg (15, register_offsets
, false);
1169 if (CSKY_16_PUSH_LIST1 (insn
))
1171 int num
= CSKY_16_PUSH_LIST1 (insn
);
1173 stacksize
+= num
* 4;
1177 fprintf_unfiltered (gdb_stdlog
,
1178 "csky: push regs_array: r4-r%d\n",
1181 for (rn
= 4; rn
<= 4 + num
- 1; rn
++)
1183 register_offsets
[rn
] = stacksize
- tmp
;
1186 fprintf_unfiltered (gdb_stdlog
,
1187 "csky: r%d saved at 0x%x"
1188 " (offset %d)\n", rn
,
1189 register_offsets
[rn
], offset
);
1195 framesize
= stacksize
;
1197 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
1200 else if (CSKY_16_IS_LRW4 (insn
) || CSKY_16_IS_MOVI4 (insn
))
1207 fprintf_unfiltered (gdb_stdlog
,
1208 "csky: looking at large frame\n");
1210 if (CSKY_16_IS_LRW4 (insn
))
1212 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1213 int offset
= ((insn
& 0x300) >> 3) | (insn
& 0x1f);
1214 int literal_addr
= (addr
+ ( offset
<< 2)) & 0xfffffffc;
1215 adjust
= read_memory_unsigned_integer (literal_addr
, 4,
1220 /* CSKY_16_IS_MOVI4 (insn). */
1221 adjust
= (insn
& 0xff);
1226 fprintf_unfiltered (gdb_stdlog
,
1227 "csky: base stacksize=0x%x\n", adjust
);
1230 /* May have zero or more instructions which modify r4. */
1233 fprintf_unfiltered (gdb_stdlog
,
1234 "csky: looking for r4 adjusters...\n");
1237 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
1238 while (CSKY_IS_R4_ADJUSTER (insn2
))
1240 if (CSKY_32_IS_ADDI4 (insn2
))
1242 int imm
= (insn2
& 0xfff) + 1;
1246 fprintf_unfiltered (gdb_stdlog
,
1247 "csky: addi r4,%d\n", imm
);
1250 else if (CSKY_32_IS_SUBI4 (insn2
))
1252 int imm
= (insn2
& 0xfff) + 1;
1256 fprintf_unfiltered (gdb_stdlog
,
1257 "csky: subi r4,%d\n", imm
);
1260 else if (CSKY_32_IS_NOR4 (insn2
))
1265 fprintf_unfiltered (gdb_stdlog
,
1266 "csky: nor r4,r4,r4\n");
1269 else if (CSKY_32_IS_ROTLI4 (insn2
))
1271 int imm
= ((insn2
>> 21) & 0x1f);
1272 int temp
= adjust
>> (32 - imm
);
1277 fprintf_unfiltered (gdb_stdlog
,
1278 "csky: rotli r4,r4,%d\n", imm
);
1281 else if (CSKY_32_IS_LISI4 (insn2
))
1283 int imm
= ((insn2
>> 21) & 0x1f);
1287 fprintf_unfiltered (gdb_stdlog
,
1288 "csky: lsli r4,r4,%d\n", imm
);
1291 else if (CSKY_32_IS_BSETI4 (insn2
))
1293 int imm
= ((insn2
>> 21) & 0x1f);
1294 adjust
|= (1 << imm
);
1297 fprintf_unfiltered (gdb_stdlog
,
1298 "csky: bseti r4,r4 %d\n", imm
);
1301 else if (CSKY_32_IS_BCLRI4 (insn2
))
1303 int imm
= ((insn2
>> 21) & 0x1f);
1304 adjust
&= ~(1 << imm
);
1307 fprintf_unfiltered (gdb_stdlog
,
1308 "csky: bclri r4,r4 %d\n", imm
);
1311 else if (CSKY_32_IS_IXH4 (insn2
))
1316 fprintf_unfiltered (gdb_stdlog
,
1317 "csky: ixh r4,r4,r4\n");
1320 else if (CSKY_32_IS_IXW4 (insn2
))
1325 fprintf_unfiltered (gdb_stdlog
,
1326 "csky: ixw r4,r4,r4\n");
1329 else if (CSKY_16_IS_ADDI4 (insn2
))
1331 int imm
= (insn2
& 0xff) + 1;
1335 fprintf_unfiltered (gdb_stdlog
,
1336 "csky: addi r4,%d\n", imm
);
1339 else if (CSKY_16_IS_SUBI4 (insn2
))
1341 int imm
= (insn2
& 0xff) + 1;
1345 fprintf_unfiltered (gdb_stdlog
,
1346 "csky: subi r4,%d\n", imm
);
1349 else if (CSKY_16_IS_NOR4 (insn2
))
1354 fprintf_unfiltered (gdb_stdlog
,
1355 "csky: nor r4,r4\n");
1358 else if (CSKY_16_IS_BSETI4 (insn2
))
1360 int imm
= (insn2
& 0x1f);
1361 adjust
|= (1 << imm
);
1364 fprintf_unfiltered (gdb_stdlog
,
1365 "csky: bseti r4, %d\n", imm
);
1368 else if (CSKY_16_IS_BCLRI4 (insn2
))
1370 int imm
= (insn2
& 0x1f);
1371 adjust
&= ~(1 << imm
);
1374 fprintf_unfiltered (gdb_stdlog
,
1375 "csky: bclri r4, %d\n", imm
);
1378 else if (CSKY_16_IS_LSLI4 (insn2
))
1380 int imm
= (insn2
& 0x1f);
1384 fprintf_unfiltered (gdb_stdlog
,
1385 "csky: lsli r4,r4, %d\n", imm
);
1390 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
1395 fprintf_unfiltered (gdb_stdlog
, "csky: "
1396 "done looking for r4 adjusters\n");
1399 /* If the next instruction adjusts the stack pointer, we keep
1400 everything; if not, we scrap it and we've found the end
1402 if (CSKY_IS_SUBU4 (insn2
))
1405 stacksize
+= adjust
;
1408 fprintf_unfiltered (gdb_stdlog
, "csky: "
1409 "found stack adjustment of 0x%x"
1410 " bytes.\n", adjust
);
1411 fprintf_unfiltered (gdb_stdlog
, "csky: "
1412 "skipping to new address %s\n",
1413 core_addr_to_string_nz (addr
));
1414 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
1419 /* None of these instructions are prologue, so don't touch
1423 fprintf_unfiltered (gdb_stdlog
, "csky: no subu sp,r4; "
1424 "NOT altering stacksize.\n");
1430 /* This is not a prologue instruction, so stop here. */
1433 fprintf_unfiltered (gdb_stdlog
, "csky: insn is not a prologue"
1434 " insn -- ending scan\n");
1441 CORE_ADDR unwound_fp
;
1442 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1443 this_cache
->framesize
= framesize
;
1447 this_cache
->framereg
= CSKY_FP_REGNUM
;
1448 unwound_fp
= get_frame_register_unsigned (this_frame
,
1449 this_cache
->framereg
);
1450 this_cache
->prev_sp
= unwound_fp
+ adjust_fp
;
1454 this_cache
->framereg
= CSKY_SP_REGNUM
;
1455 unwound_fp
= get_frame_register_unsigned (this_frame
,
1456 this_cache
->framereg
);
1457 this_cache
->prev_sp
= unwound_fp
+ stacksize
;
1460 /* Note where saved registers are stored. The offsets in
1461 REGISTER_OFFSETS are computed relative to the top of the frame. */
1462 for (rn
= 0; rn
< CSKY_NUM_GREGS
; rn
++)
1464 if (register_offsets
[rn
] >= 0)
1466 this_cache
->saved_regs
[rn
].addr
1467 = this_cache
->prev_sp
- register_offsets
[rn
];
1470 CORE_ADDR rn_value
= read_memory_unsigned_integer (
1471 this_cache
->saved_regs
[rn
].addr
, 4, byte_order
);
1472 fprintf_unfiltered (gdb_stdlog
, "Saved register %s "
1473 "stored at 0x%08lx, value=0x%08lx\n",
1474 csky_register_names
[rn
],
1476 this_cache
->saved_regs
[rn
].addr
,
1477 (unsigned long) rn_value
);
1481 if (lr_type
== LR_TYPE_EPC
)
1484 this_cache
->saved_regs
[CSKY_PC_REGNUM
]
1485 = this_cache
->saved_regs
[CSKY_EPC_REGNUM
];
1487 else if (lr_type
== LR_TYPE_FPC
)
1490 this_cache
->saved_regs
[CSKY_PC_REGNUM
]
1491 = this_cache
->saved_regs
[CSKY_FPC_REGNUM
];
1495 this_cache
->saved_regs
[CSKY_PC_REGNUM
]
1496 = this_cache
->saved_regs
[CSKY_LR_REGNUM
];
1503 /* Detect whether PC is at a point where the stack frame has been
1507 csky_stack_frame_destroyed_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1511 CORE_ADDR func_start
, func_end
;
1513 if (!find_pc_partial_function (pc
, NULL
, &func_start
, &func_end
))
1516 bool fp_saved
= false;
1518 for (addr
= func_start
; addr
< func_end
; addr
+= insn_len
)
1520 /* Get next insn. */
1521 insn_len
= csky_get_insn (gdbarch
, addr
, &insn
);
1525 /* Is sp is saved to fp. */
1526 if (CSKY_16_IS_MOV_FP_SP (insn
))
1528 /* If sp was saved to fp and now being restored from
1529 fp then it indicates the start of epilog. */
1530 else if (fp_saved
&& CSKY_16_IS_MOV_SP_FP (insn
))
1537 /* Implement the skip_prologue gdbarch hook. */
1540 csky_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1542 CORE_ADDR func_addr
, func_end
;
1543 struct symtab_and_line sal
;
1544 const int default_search_limit
= 128;
1546 /* See if we can find the end of the prologue using the symbol table. */
1547 if (find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
1549 CORE_ADDR post_prologue_pc
1550 = skip_prologue_using_sal (gdbarch
, func_addr
);
1552 if (post_prologue_pc
!= 0)
1553 return std::max (pc
, post_prologue_pc
);
1556 func_end
= pc
+ default_search_limit
;
1558 /* Find the end of prologue. Default lr_type. */
1559 return csky_analyze_prologue (gdbarch
, pc
, func_end
, func_end
,
1560 NULL
, NULL
, LR_TYPE_R15
);
1563 /* Implement the breakpoint_kind_from_pc gdbarch method. */
1566 csky_breakpoint_kind_from_pc (struct gdbarch
*gdbarch
, CORE_ADDR
*pcptr
)
1568 if (csky_pc_is_csky16 (gdbarch
, *pcptr
))
1569 return CSKY_INSN_SIZE16
;
1571 return CSKY_INSN_SIZE32
;
1574 /* Implement the sw_breakpoint_from_kind gdbarch method. */
1576 static const gdb_byte
*
1577 csky_sw_breakpoint_from_kind (struct gdbarch
*gdbarch
, int kind
, int *size
)
1580 if (kind
== CSKY_INSN_SIZE16
)
1582 static gdb_byte csky_16_breakpoint
[] = { 0, 0 };
1583 return csky_16_breakpoint
;
1587 static gdb_byte csky_32_breakpoint
[] = { 0, 0, 0, 0 };
1588 return csky_32_breakpoint
;
1592 /* Implement the memory_insert_breakpoint gdbarch method. */
1595 csky_memory_insert_breakpoint (struct gdbarch
*gdbarch
,
1596 struct bp_target_info
*bp_tgt
)
1599 const unsigned char *bp
;
1600 gdb_byte bp_write_record1
[] = { 0, 0, 0, 0 };
1601 gdb_byte bp_write_record2
[] = { 0, 0, 0, 0 };
1602 gdb_byte bp_record
[] = { 0, 0, 0, 0 };
1604 /* Sanity-check bp_address. */
1605 if (bp_tgt
->reqstd_address
% 2)
1606 warning (_("Invalid breakpoint address 0x%x is an odd number."),
1607 (unsigned int) bp_tgt
->reqstd_address
);
1608 scoped_restore restore_memory
1609 = make_scoped_restore_show_memory_breakpoints (1);
1611 /* Determine appropriate breakpoint_kind for this address. */
1612 bp_tgt
->kind
= csky_breakpoint_kind_from_pc (gdbarch
,
1613 &bp_tgt
->reqstd_address
);
1615 /* Save the memory contents. */
1616 bp_tgt
->shadow_len
= bp_tgt
->kind
;
1618 /* Fill bp_tgt->placed_address. */
1619 bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
1621 if (bp_tgt
->kind
== CSKY_INSN_SIZE16
)
1623 if ((bp_tgt
->reqstd_address
% 4) == 0)
1625 /* Read two bytes. */
1626 val
= target_read_memory (bp_tgt
->reqstd_address
,
1627 bp_tgt
->shadow_contents
, 2);
1631 /* Read two bytes. */
1632 val
= target_read_memory (bp_tgt
->reqstd_address
+ 2,
1637 /* Write the breakpoint. */
1638 bp_write_record1
[2] = bp_record
[0];
1639 bp_write_record1
[3] = bp_record
[1];
1640 bp
= bp_write_record1
;
1641 val
= target_write_raw_memory (bp_tgt
->reqstd_address
, bp
,
1646 val
= target_read_memory (bp_tgt
->reqstd_address
,
1647 bp_tgt
->shadow_contents
, 2);
1651 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1656 /* Write the breakpoint. */
1657 bp_write_record1
[0] = bp_record
[0];
1658 bp_write_record1
[1] = bp_record
[1];
1659 bp
= bp_write_record1
;
1660 val
= target_write_raw_memory (bp_tgt
->reqstd_address
- 2,
1661 bp
, CSKY_WR_BKPT_MODE
);
1666 if (bp_tgt
->placed_address
% 4 == 0)
1668 val
= target_read_memory (bp_tgt
->reqstd_address
,
1669 bp_tgt
->shadow_contents
,
1674 /* Write the breakpoint. */
1675 bp
= bp_write_record1
;
1676 val
= target_write_raw_memory (bp_tgt
->reqstd_address
,
1677 bp
, CSKY_WR_BKPT_MODE
);
1681 val
= target_read_memory (bp_tgt
->reqstd_address
,
1682 bp_tgt
->shadow_contents
,
1687 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1692 val
= target_read_memory (bp_tgt
->reqstd_address
+ 4,
1697 bp_write_record1
[0] = bp_record
[0];
1698 bp_write_record1
[1] = bp_record
[1];
1699 bp_write_record2
[2] = bp_record
[2];
1700 bp_write_record2
[3] = bp_record
[3];
1702 /* Write the breakpoint. */
1703 bp
= bp_write_record1
;
1704 val
= target_write_raw_memory (bp_tgt
->reqstd_address
- 2, bp
,
1709 /* Write the breakpoint. */
1710 bp
= bp_write_record2
;
1711 val
= target_write_raw_memory (bp_tgt
->reqstd_address
+ 2, bp
,
1718 /* Restore the breakpoint shadow_contents to the target. */
1721 csky_memory_remove_breakpoint (struct gdbarch
*gdbarch
,
1722 struct bp_target_info
*bp_tgt
)
1725 gdb_byte bp_record
[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1726 /* Different for shadow_len 2 or 4. */
1727 if (bp_tgt
->shadow_len
== 2)
1729 /* Do word-sized writes on word-aligned boundaries and read
1730 padding bytes as necessary. */
1731 if (bp_tgt
->reqstd_address
% 4 == 0)
1733 val
= target_read_memory (bp_tgt
->reqstd_address
+ 2,
1737 bp_record
[0] = bp_tgt
->shadow_contents
[0];
1738 bp_record
[1] = bp_tgt
->shadow_contents
[1];
1739 return target_write_raw_memory (bp_tgt
->reqstd_address
,
1740 bp_record
, CSKY_WR_BKPT_MODE
);
1744 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1748 bp_record
[2] = bp_tgt
->shadow_contents
[0];
1749 bp_record
[3] = bp_tgt
->shadow_contents
[1];
1750 return target_write_raw_memory (bp_tgt
->reqstd_address
- 2,
1751 bp_record
, CSKY_WR_BKPT_MODE
);
1756 /* Do word-sized writes on word-aligned boundaries and read
1757 padding bytes as necessary. */
1758 if (bp_tgt
->placed_address
% 4 == 0)
1760 return target_write_raw_memory (bp_tgt
->reqstd_address
,
1761 bp_tgt
->shadow_contents
,
1766 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1770 val
= target_read_memory (bp_tgt
->reqstd_address
+ 4,
1775 bp_record
[2] = bp_tgt
->shadow_contents
[0];
1776 bp_record
[3] = bp_tgt
->shadow_contents
[1];
1777 bp_record
[4] = bp_tgt
->shadow_contents
[2];
1778 bp_record
[5] = bp_tgt
->shadow_contents
[3];
1780 return target_write_raw_memory (bp_tgt
->reqstd_address
- 2,
1782 CSKY_WR_BKPT_MODE
* 2);
1787 /* Determine link register type. */
1790 csky_analyze_lr_type (struct gdbarch
*gdbarch
,
1791 CORE_ADDR start_pc
, CORE_ADDR end_pc
)
1794 unsigned int insn
, insn_len
;
1797 for (addr
= start_pc
; addr
< end_pc
; addr
+= insn_len
)
1799 insn_len
= csky_get_insn (gdbarch
, addr
, &insn
);
1802 if (CSKY_32_IS_MFCR_EPSR (insn
) || CSKY_32_IS_MFCR_EPC (insn
)
1803 || CSKY_32_IS_RTE (insn
))
1806 else if (CSKY_32_IS_MFCR_FPSR (insn
) || CSKY_32_IS_MFCR_FPC (insn
)
1807 || CSKY_32_IS_RFI (insn
))
1809 else if (CSKY_32_IS_JMP (insn
) || CSKY_32_IS_BR (insn
)
1810 || CSKY_32_IS_JMPIX (insn
) || CSKY_32_IS_JMPI (insn
))
1814 /* 16 bit instruction. */
1815 if (CSKY_16_IS_JMP (insn
) || CSKY_16_IS_BR (insn
)
1816 || CSKY_16_IS_JMPIX (insn
))
1823 /* Heuristic unwinder. */
1825 static struct csky_unwind_cache
*
1826 csky_frame_unwind_cache (struct frame_info
*this_frame
)
1828 CORE_ADDR prologue_start
, prologue_end
, func_end
, prev_pc
, block_addr
;
1829 struct csky_unwind_cache
*cache
;
1830 const struct block
*bl
;
1831 unsigned long func_size
= 0;
1832 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1833 unsigned int sp_regnum
= CSKY_SP_REGNUM
;
1835 /* Default lr type is r15. */
1836 lr_type_t lr_type
= LR_TYPE_R15
;
1838 cache
= FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache
);
1839 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
1841 /* Assume there is no frame until proven otherwise. */
1842 cache
->framereg
= sp_regnum
;
1844 cache
->framesize
= 0;
1846 prev_pc
= get_frame_pc (this_frame
);
1847 block_addr
= get_frame_address_in_block (this_frame
);
1848 if (find_pc_partial_function (block_addr
, NULL
, &prologue_start
,
1850 /* We couldn't find a function containing block_addr, so bail out
1851 and hope for the best. */
1854 /* Get the (function) symbol matching prologue_start. */
1855 bl
= block_for_pc (prologue_start
);
1857 func_size
= bl
->endaddr
- bl
->startaddr
;
1860 struct bound_minimal_symbol msymbol
1861 = lookup_minimal_symbol_by_pc (prologue_start
);
1862 if (msymbol
.minsym
!= NULL
)
1863 func_size
= MSYMBOL_SIZE (msymbol
.minsym
);
1866 /* If FUNC_SIZE is 0 we may have a special-case use of lr
1867 e.g. exception or interrupt. */
1869 lr_type
= csky_analyze_lr_type (gdbarch
, prologue_start
, func_end
);
1871 prologue_end
= std::min (func_end
, prev_pc
);
1873 /* Analyze the function prologue. */
1874 csky_analyze_prologue (gdbarch
, prologue_start
, prologue_end
,
1875 func_end
, this_frame
, cache
, lr_type
);
1877 /* gdbarch_sp_regnum contains the value and not the address. */
1878 trad_frame_set_value (cache
->saved_regs
, sp_regnum
, cache
->prev_sp
);
1882 /* Implement the this_id function for the normal unwinder. */
1885 csky_frame_this_id (struct frame_info
*this_frame
,
1886 void **this_prologue_cache
, struct frame_id
*this_id
)
1888 struct csky_unwind_cache
*cache
;
1891 if (*this_prologue_cache
== NULL
)
1892 *this_prologue_cache
= csky_frame_unwind_cache (this_frame
);
1893 cache
= (struct csky_unwind_cache
*) *this_prologue_cache
;
1895 /* This marks the outermost frame. */
1896 if (cache
->prev_sp
== 0)
1899 id
= frame_id_build (cache
->prev_sp
, get_frame_func (this_frame
));
1903 /* Implement the prev_register function for the normal unwinder. */
1905 static struct value
*
1906 csky_frame_prev_register (struct frame_info
*this_frame
,
1907 void **this_prologue_cache
, int regnum
)
1909 struct csky_unwind_cache
*cache
;
1911 if (*this_prologue_cache
== NULL
)
1912 *this_prologue_cache
= csky_frame_unwind_cache (this_frame
);
1913 cache
= (struct csky_unwind_cache
*) *this_prologue_cache
;
1915 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
,
1919 /* Data structures for the normal prologue-analysis-based
1922 static const struct frame_unwind csky_unwind_cache
= {
1924 default_frame_unwind_stop_reason
,
1926 csky_frame_prev_register
,
1928 default_frame_sniffer
,
1936 csky_stub_unwind_sniffer (const struct frame_unwind
*self
,
1937 struct frame_info
*this_frame
,
1938 void **this_prologue_cache
)
1940 CORE_ADDR addr_in_block
;
1942 addr_in_block
= get_frame_address_in_block (this_frame
);
1944 if (find_pc_partial_function (addr_in_block
, NULL
, NULL
, NULL
) == 0
1945 || in_plt_section (addr_in_block
))
1951 static struct csky_unwind_cache
*
1952 csky_make_stub_cache (struct frame_info
*this_frame
)
1954 struct csky_unwind_cache
*cache
;
1956 cache
= FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache
);
1957 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
1958 cache
->prev_sp
= get_frame_register_unsigned (this_frame
, CSKY_SP_REGNUM
);
1964 csky_stub_this_id (struct frame_info
*this_frame
,
1966 struct frame_id
*this_id
)
1968 struct csky_unwind_cache
*cache
;
1970 if (*this_cache
== NULL
)
1971 *this_cache
= csky_make_stub_cache (this_frame
);
1972 cache
= (struct csky_unwind_cache
*) *this_cache
;
1974 /* Our frame ID for a stub frame is the current SP and LR. */
1975 *this_id
= frame_id_build (cache
->prev_sp
, get_frame_pc (this_frame
));
1978 static struct value
*
1979 csky_stub_prev_register (struct frame_info
*this_frame
,
1983 struct csky_unwind_cache
*cache
;
1985 if (*this_cache
== NULL
)
1986 *this_cache
= csky_make_stub_cache (this_frame
);
1987 cache
= (struct csky_unwind_cache
*) *this_cache
;
1989 /* If we are asked to unwind the PC, then return the LR. */
1990 if (prev_regnum
== CSKY_PC_REGNUM
)
1994 lr
= frame_unwind_register_unsigned (this_frame
, CSKY_LR_REGNUM
);
1995 return frame_unwind_got_constant (this_frame
, prev_regnum
, lr
);
1998 if (prev_regnum
== CSKY_SP_REGNUM
)
1999 return frame_unwind_got_constant (this_frame
, prev_regnum
, cache
->prev_sp
);
2001 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
,
2005 struct frame_unwind csky_stub_unwind
= {
2007 default_frame_unwind_stop_reason
,
2009 csky_stub_prev_register
,
2011 csky_stub_unwind_sniffer
2014 /* Implement the this_base, this_locals, and this_args hooks
2015 for the normal unwinder. */
2018 csky_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
2020 struct csky_unwind_cache
*cache
;
2022 if (*this_cache
== NULL
)
2023 *this_cache
= csky_frame_unwind_cache (this_frame
);
2024 cache
= (struct csky_unwind_cache
*) *this_cache
;
2026 return cache
->prev_sp
- cache
->framesize
;
2029 static const struct frame_base csky_frame_base
= {
2031 csky_frame_base_address
,
2032 csky_frame_base_address
,
2033 csky_frame_base_address
2036 /* Initialize register access method. */
2039 csky_dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
2040 struct dwarf2_frame_state_reg
*reg
,
2041 struct frame_info
*this_frame
)
2043 if (regnum
== gdbarch_pc_regnum (gdbarch
))
2044 reg
->how
= DWARF2_FRAME_REG_RA
;
2045 else if (regnum
== gdbarch_sp_regnum (gdbarch
))
2046 reg
->how
= DWARF2_FRAME_REG_CFA
;
2049 /* Create csky register groups. */
2052 csky_init_reggroup ()
2054 cr_reggroup
= reggroup_new ("cr", USER_REGGROUP
);
2055 fr_reggroup
= reggroup_new ("fr", USER_REGGROUP
);
2056 vr_reggroup
= reggroup_new ("vr", USER_REGGROUP
);
2057 mmu_reggroup
= reggroup_new ("mmu", USER_REGGROUP
);
2058 prof_reggroup
= reggroup_new ("profiling", USER_REGGROUP
);
2061 /* Add register groups into reggroup list. */
2064 csky_add_reggroups (struct gdbarch
*gdbarch
)
2066 reggroup_add (gdbarch
, all_reggroup
);
2067 reggroup_add (gdbarch
, general_reggroup
);
2068 reggroup_add (gdbarch
, cr_reggroup
);
2069 reggroup_add (gdbarch
, fr_reggroup
);
2070 reggroup_add (gdbarch
, vr_reggroup
);
2071 reggroup_add (gdbarch
, mmu_reggroup
);
2072 reggroup_add (gdbarch
, prof_reggroup
);
2075 /* Return the groups that a CSKY register can be categorised into. */
2078 csky_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
2079 struct reggroup
*reggroup
)
2083 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
2084 || gdbarch_register_name (gdbarch
, regnum
)[0] == '\0')
2087 if (reggroup
== all_reggroup
)
2090 raw_p
= regnum
< gdbarch_num_regs (gdbarch
);
2091 if (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
)
2094 if (((regnum
>= CSKY_R0_REGNUM
) && (regnum
<= CSKY_R0_REGNUM
+ 31))
2095 && (reggroup
== general_reggroup
))
2098 if (((regnum
== CSKY_PC_REGNUM
)
2099 || ((regnum
>= CSKY_CR0_REGNUM
)
2100 && (regnum
<= CSKY_CR0_REGNUM
+ 30)))
2101 && (reggroup
== cr_reggroup
))
2104 if ((((regnum
>= CSKY_VR0_REGNUM
) && (regnum
<= CSKY_VR0_REGNUM
+ 15))
2105 || ((regnum
>= CSKY_VCR0_REGNUM
)
2106 && (regnum
<= CSKY_VCR0_REGNUM
+ 2)))
2107 && (reggroup
== vr_reggroup
))
2110 if (((regnum
>= CSKY_MMU_REGNUM
) && (regnum
<= CSKY_MMU_REGNUM
+ 8))
2111 && (reggroup
== mmu_reggroup
))
2114 if (((regnum
>= CSKY_PROFCR_REGNUM
)
2115 && (regnum
<= CSKY_PROFCR_REGNUM
+ 48))
2116 && (reggroup
== prof_reggroup
))
2119 if ((((regnum
>= CSKY_FR0_REGNUM
) && (regnum
<= CSKY_FR0_REGNUM
+ 15))
2120 || ((regnum
>= CSKY_VCR0_REGNUM
) && (regnum
<= CSKY_VCR0_REGNUM
+ 2)))
2121 && (reggroup
== fr_reggroup
))
2127 /* Implement the dwarf2_reg_to_regnum gdbarch method. */
2130 csky_dwarf_reg_to_regnum (struct gdbarch
*gdbarch
, int dw_reg
)
2132 if (dw_reg
< 0 || dw_reg
>= CSKY_NUM_REGS
)
2137 /* Override interface for command: info register. */
2140 csky_print_registers_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
2141 struct frame_info
*frame
, int regnum
, int all
)
2143 /* Call default print_registers_info function. */
2144 default_print_registers_info (gdbarch
, file
, frame
, regnum
, all
);
2146 /* For command: info register. */
2147 if (regnum
== -1 && all
== 0)
2149 default_print_registers_info (gdbarch
, file
, frame
,
2151 default_print_registers_info (gdbarch
, file
, frame
,
2152 CSKY_EPC_REGNUM
, 0);
2153 default_print_registers_info (gdbarch
, file
, frame
,
2154 CSKY_CR0_REGNUM
, 0);
2155 default_print_registers_info (gdbarch
, file
, frame
,
2156 CSKY_EPSR_REGNUM
, 0);
2161 /* Initialize the current architecture based on INFO. If possible,
2162 re-use an architecture from ARCHES, which is a list of
2163 architectures already created during this debugging session.
2165 Called at program startup, when reading a core file, and when
2166 reading a binary file. */
2168 static struct gdbarch
*
2169 csky_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
2171 struct gdbarch
*gdbarch
;
2172 struct gdbarch_tdep
*tdep
;
2174 /* Find a candidate among the list of pre-declared architectures. */
2175 arches
= gdbarch_list_lookup_by_info (arches
, &info
);
2177 return arches
->gdbarch
;
2179 /* None found, create a new architecture from the information
2181 tdep
= XCNEW (struct gdbarch_tdep
);
2182 gdbarch
= gdbarch_alloc (&info
, tdep
);
2184 /* Target data types. */
2185 set_gdbarch_ptr_bit (gdbarch
, 32);
2186 set_gdbarch_addr_bit (gdbarch
, 32);
2187 set_gdbarch_short_bit (gdbarch
, 16);
2188 set_gdbarch_int_bit (gdbarch
, 32);
2189 set_gdbarch_long_bit (gdbarch
, 32);
2190 set_gdbarch_long_long_bit (gdbarch
, 64);
2191 set_gdbarch_float_bit (gdbarch
, 32);
2192 set_gdbarch_double_bit (gdbarch
, 64);
2193 set_gdbarch_float_format (gdbarch
, floatformats_ieee_single
);
2194 set_gdbarch_double_format (gdbarch
, floatformats_ieee_double
);
2196 /* Information about the target architecture. */
2197 set_gdbarch_return_value (gdbarch
, csky_return_value
);
2198 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, csky_breakpoint_kind_from_pc
);
2199 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, csky_sw_breakpoint_from_kind
);
2201 /* Register architecture. */
2202 set_gdbarch_num_regs (gdbarch
, CSKY_NUM_REGS
);
2203 set_gdbarch_pc_regnum (gdbarch
, CSKY_PC_REGNUM
);
2204 set_gdbarch_sp_regnum (gdbarch
, CSKY_SP_REGNUM
);
2205 set_gdbarch_register_name (gdbarch
, csky_register_name
);
2206 set_gdbarch_register_type (gdbarch
, csky_register_type
);
2207 set_gdbarch_read_pc (gdbarch
, csky_read_pc
);
2208 set_gdbarch_write_pc (gdbarch
, csky_write_pc
);
2209 set_gdbarch_print_registers_info (gdbarch
, csky_print_registers_info
);
2210 csky_add_reggroups (gdbarch
);
2211 set_gdbarch_register_reggroup_p (gdbarch
, csky_register_reggroup_p
);
2212 set_gdbarch_stab_reg_to_regnum (gdbarch
, csky_dwarf_reg_to_regnum
);
2213 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, csky_dwarf_reg_to_regnum
);
2214 dwarf2_frame_set_init_reg (gdbarch
, csky_dwarf2_frame_init_reg
);
2216 /* Functions to analyze frames. */
2217 frame_base_set_default (gdbarch
, &csky_frame_base
);
2218 set_gdbarch_skip_prologue (gdbarch
, csky_skip_prologue
);
2219 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
2220 set_gdbarch_frame_align (gdbarch
, csky_frame_align
);
2221 set_gdbarch_stack_frame_destroyed_p (gdbarch
, csky_stack_frame_destroyed_p
);
2223 /* Functions handling dummy frames. */
2224 set_gdbarch_push_dummy_call (gdbarch
, csky_push_dummy_call
);
2226 /* Frame unwinders. Use DWARF debug info if available,
2227 otherwise use our own unwinder. */
2228 dwarf2_append_unwinders (gdbarch
);
2229 frame_unwind_append_unwinder (gdbarch
, &csky_stub_unwind
);
2230 frame_unwind_append_unwinder (gdbarch
, &csky_unwind_cache
);
2233 set_gdbarch_memory_insert_breakpoint (gdbarch
,
2234 csky_memory_insert_breakpoint
);
2235 set_gdbarch_memory_remove_breakpoint (gdbarch
,
2236 csky_memory_remove_breakpoint
);
2238 /* Hook in ABI-specific overrides, if they have been registered. */
2239 gdbarch_init_osabi (info
, gdbarch
);
2241 /* Support simple overlay manager. */
2242 set_gdbarch_overlay_update (gdbarch
, simple_overlay_update
);
2243 set_gdbarch_char_signed (gdbarch
, 0);
2248 _initialize_csky_tdep (void)
2251 register_gdbarch_init (bfd_arch_csky
, csky_gdbarch_init
);
2253 csky_init_reggroup ();
2255 /* Allow debugging this file's internals. */
2256 add_setshow_boolean_cmd ("csky", class_maintenance
, &csky_debug
,
2257 _("Set C-Sky debugging."),
2258 _("Show C-Sky debugging."),
2259 _("When on, C-Sky specific debugging is enabled."),
2262 &setdebuglist
, &showdebuglist
);