1 /* Target-dependent code for the CSKY architecture, for GDB.
3 Copyright (C) 2010-2021 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/>. */
23 #include "gdbsupport/gdb_assert.h"
35 #include "arch-utils.h"
39 #include "reggroups.h"
43 #include "sim-regno.h"
45 #include "frame-unwind.h"
46 #include "frame-base.h"
47 #include "trad-frame.h"
49 #include "floatformat.h"
51 #include "target-descriptions.h"
52 #include "dwarf2/frame.h"
53 #include "user-regs.h"
55 #include "csky-tdep.h"
57 #include "opcode/csky.h"
61 /* Control debugging information emitted in this file. */
62 static bool csky_debug
= false;
64 static struct reggroup
*cr_reggroup
;
65 static struct reggroup
*fr_reggroup
;
66 static struct reggroup
*vr_reggroup
;
67 static struct reggroup
*mmu_reggroup
;
68 static struct reggroup
*prof_reggroup
;
70 /* Convenience function to print debug messages in prologue analysis. */
73 print_savedreg_msg (int regno
, int offsets
[], bool print_continuing
)
75 fprintf_unfiltered (gdb_stdlog
, "csky: r%d saved at offset 0x%x\n",
76 regno
, offsets
[regno
]);
78 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
81 /* Check whether the instruction at ADDR is 16-bit or not. */
84 csky_pc_is_csky16 (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
86 gdb_byte target_mem
[2];
90 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
92 status
= target_read_memory (addr
, target_mem
, 2);
93 /* Assume a 16-bit instruction if we can't read memory. */
97 /* Get instruction from memory. */
98 insn
= extract_unsigned_integer (target_mem
, 2, byte_order
);
99 if ((insn
& CSKY_32_INSN_MASK
) == CSKY_32_INSN_MASK
)
101 else if (insn
== CSKY_BKPT_INSN
)
103 /* Check for 32-bit bkpt instruction which is all 0. */
104 status
= target_read_memory (addr
+ 2, target_mem
, 2);
108 insn
= extract_unsigned_integer (target_mem
, 2, byte_order
);
109 if (insn
== CSKY_BKPT_INSN
)
115 /* Get one instruction at ADDR and store it in INSN. Return 2 for
116 a 16-bit instruction or 4 for a 32-bit instruction. */
119 csky_get_insn (struct gdbarch
*gdbarch
, CORE_ADDR addr
, unsigned int *insn
)
121 gdb_byte target_mem
[2];
122 unsigned int insn_type
;
125 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
127 status
= target_read_memory (addr
, target_mem
, 2);
129 memory_error (TARGET_XFER_E_IO
, addr
);
131 insn_type
= extract_unsigned_integer (target_mem
, 2, byte_order
);
132 if (CSKY_32_INSN_MASK
== (insn_type
& CSKY_32_INSN_MASK
))
134 status
= target_read_memory (addr
+ 2, target_mem
, 2);
136 memory_error (TARGET_XFER_E_IO
, addr
);
137 insn_type
= ((insn_type
<< 16)
138 | extract_unsigned_integer (target_mem
, 2, byte_order
));
145 /* Implement the read_pc gdbarch method. */
148 csky_read_pc (readable_regcache
*regcache
)
151 regcache
->cooked_read (CSKY_PC_REGNUM
, &pc
);
155 /* Implement the write_pc gdbarch method. */
158 csky_write_pc (regcache
*regcache
, CORE_ADDR val
)
160 regcache_cooked_write_unsigned (regcache
, CSKY_PC_REGNUM
, val
);
163 /* C-Sky ABI register names. */
165 static const char * const csky_register_names
[] =
167 /* General registers 0 - 31. */
168 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
169 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
170 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
171 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
173 /* DSP hilo registers 36 and 37. */
174 "", "", "", "", "hi", "lo", "", "",
176 /* FPU/VPU general registers 40 - 71. */
177 "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
178 "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
179 "vr0", "vr1", "vr2", "vr3", "vr4", "vr5", "vr6", "vr7",
180 "vr8", "vr9", "vr10", "vr11", "vr12", "vr13", "vr14", "vr15",
182 /* Program counter 72. */
185 /* Optional registers (ar) 73 - 88. */
186 "ar0", "ar1", "ar2", "ar3", "ar4", "ar5", "ar6", "ar7",
187 "ar8", "ar9", "ar10", "ar11", "ar12", "ar13", "ar14", "ar15",
189 /* Control registers (cr) 89 - 119. */
190 "psr", "vbr", "epsr", "fpsr", "epc", "fpc", "ss0", "ss1",
191 "ss2", "ss3", "ss4", "gcr", "gsr", "cr13", "cr14", "cr15",
192 "cr16", "cr17", "cr18", "cr19", "cr20", "cr21", "cr22", "cr23",
193 "cr24", "cr25", "cr26", "cr27", "cr28", "cr29", "cr30", "cr31",
195 /* FPU/VPU control registers 121 ~ 123. */
197 "fid", "fcr", "fesr", "", "", "", "usp",
199 /* MMU control registers: 128 - 136. */
200 "mcr0", "mcr2", "mcr3", "mcr4", "mcr6", "mcr8", "mcr29", "mcr30",
203 /* Profiling control registers 140 - 143. */
204 /* Profiling software general registers 144 - 157. */
205 "profcr0", "profcr1", "profcr2", "profcr3", "profsgr0", "profsgr1",
206 "profsgr2", "profsgr3", "profsgr4", "profsgr5", "profsgr6", "profsgr7",
207 "profsgr8", "profsgr9", "profsgr10","profsgr11","profsgr12", "profsgr13",
210 /* Profiling architecture general registers 160 - 174. */
211 "profagr0", "profagr1", "profagr2", "profagr3", "profagr4", "profagr5",
212 "profagr6", "profagr7", "profagr8", "profagr9", "profagr10","profagr11",
213 "profagr12","profagr13","profagr14", "",
215 /* Profiling extension general registers 176 - 188. */
216 "profxgr0", "profxgr1", "profxgr2", "profxgr3", "profxgr4", "profxgr5",
217 "profxgr6", "profxgr7", "profxgr8", "profxgr9", "profxgr10","profxgr11",
220 /* Control registers in bank1. */
221 "", "", "", "", "", "", "", "",
222 "", "", "", "", "", "", "", "",
223 "cp1cr16", "cp1cr17", "cp1cr18", "cp1cr19", "cp1cr20", "", "", "",
224 "", "", "", "", "", "", "", "",
226 /* Control registers in bank3 (ICE). */
227 "sepsr", "sevbr", "seepsr", "", "seepc", "", "nsssp", "seusp",
228 "sedcr", "", "", "", "", "", "", "",
229 "", "", "", "", "", "", "", "",
230 "", "", "", "", "", "", "", ""
233 /* Implement the register_name gdbarch method. */
236 csky_register_name (struct gdbarch
*gdbarch
, int reg_nr
)
241 if (reg_nr
>= gdbarch_num_regs (gdbarch
))
244 return csky_register_names
[reg_nr
];
247 /* Construct vector type for vrx registers. */
250 csky_vector_type (struct gdbarch
*gdbarch
)
252 const struct builtin_type
*bt
= builtin_type (gdbarch
);
256 t
= arch_composite_type (gdbarch
, "__gdb_builtin_type_vec128i",
259 append_composite_type_field (t
, "u32",
260 init_vector_type (bt
->builtin_int32
, 4));
261 append_composite_type_field (t
, "u16",
262 init_vector_type (bt
->builtin_int16
, 8));
263 append_composite_type_field (t
, "u8",
264 init_vector_type (bt
->builtin_int8
, 16));
266 t
->set_is_vector (true);
267 t
->set_name ("builtin_type_vec128i");
272 /* Return the GDB type object for the "standard" data type
273 of data in register N. */
276 csky_register_type (struct gdbarch
*gdbarch
, int reg_nr
)
278 /* PC, EPC, FPC is a text pointer. */
279 if ((reg_nr
== CSKY_PC_REGNUM
) || (reg_nr
== CSKY_EPC_REGNUM
)
280 || (reg_nr
== CSKY_FPC_REGNUM
))
281 return builtin_type (gdbarch
)->builtin_func_ptr
;
283 /* VBR is a data pointer. */
284 if (reg_nr
== CSKY_VBR_REGNUM
)
285 return builtin_type (gdbarch
)->builtin_data_ptr
;
287 /* Float register has 64 bits, and only in ck810. */
288 if ((reg_nr
>=CSKY_FR0_REGNUM
) && (reg_nr
<= CSKY_FR0_REGNUM
+ 15))
289 return arch_float_type (gdbarch
, 64, "builtin_type_csky_ext",
290 floatformats_ieee_double
);
292 /* Vector register has 128 bits, and only in ck810. */
293 if ((reg_nr
>= CSKY_VR0_REGNUM
) && (reg_nr
<= CSKY_VR0_REGNUM
+ 15))
294 return csky_vector_type (gdbarch
);
296 /* Profiling general register has 48 bits, we use 64bit. */
297 if ((reg_nr
>= CSKY_PROFGR_REGNUM
) && (reg_nr
<= CSKY_PROFGR_REGNUM
+ 44))
298 return builtin_type (gdbarch
)->builtin_uint64
;
300 if (reg_nr
== CSKY_SP_REGNUM
)
301 return builtin_type (gdbarch
)->builtin_data_ptr
;
303 /* Others are 32 bits. */
304 return builtin_type (gdbarch
)->builtin_int32
;
307 /* Data structure to marshall items in a dummy stack frame when
308 calling a function in the inferior. */
312 stack_item (int len_
, const gdb_byte
*data_
)
313 : len (len_
), data (data_
)
317 const gdb_byte
*data
;
320 /* Implement the push_dummy_call gdbarch method. */
323 csky_push_dummy_call (struct gdbarch
*gdbarch
, struct value
*function
,
324 struct regcache
*regcache
, CORE_ADDR bp_addr
,
325 int nargs
, struct value
**args
, CORE_ADDR sp
,
326 function_call_return_method return_method
,
327 CORE_ADDR struct_addr
)
330 int argreg
= CSKY_ABI_A0_REGNUM
;
331 int last_arg_regnum
= CSKY_ABI_LAST_ARG_REGNUM
;
332 int need_dummy_stack
= 0;
333 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
334 std::vector
<stack_item
> stack_items
;
336 /* Set the return address. For CSKY, the return breakpoint is
337 always at BP_ADDR. */
338 regcache_cooked_write_unsigned (regcache
, CSKY_LR_REGNUM
, bp_addr
);
340 /* The struct_return pointer occupies the first parameter
342 if (return_method
== return_method_struct
)
346 fprintf_unfiltered (gdb_stdlog
,
347 "csky: struct return in %s = %s\n",
348 gdbarch_register_name (gdbarch
, argreg
),
349 paddress (gdbarch
, struct_addr
));
351 regcache_cooked_write_unsigned (regcache
, argreg
, struct_addr
);
355 /* Put parameters into argument registers in REGCACHE.
356 In ABI argument registers are r0 through r3. */
357 for (argnum
= 0; argnum
< nargs
; argnum
++)
360 struct type
*arg_type
;
363 arg_type
= check_typedef (value_type (args
[argnum
]));
364 len
= TYPE_LENGTH (arg_type
);
365 val
= value_contents (args
[argnum
]).data ();
367 /* Copy the argument to argument registers or the dummy stack.
368 Large arguments are split between registers and stack.
370 If len < 4, there is no need to worry about endianness since
371 the arguments will always be stored in the low address. */
375 = extract_unsigned_integer (val
, len
, byte_order
);
376 regcache_cooked_write_unsigned (regcache
, argreg
, regval
);
383 int partial_len
= len
< 4 ? len
: 4;
384 if (argreg
<= last_arg_regnum
)
386 /* The argument is passed in an argument register. */
388 = extract_unsigned_integer (val
, partial_len
,
390 if (byte_order
== BFD_ENDIAN_BIG
)
391 regval
<<= (4 - partial_len
) * 8;
393 /* Put regval into register in REGCACHE. */
394 regcache_cooked_write_unsigned (regcache
, argreg
,
400 /* The argument should be pushed onto the dummy stack. */
401 stack_items
.emplace_back (4, val
);
402 need_dummy_stack
+= 4;
410 /* Transfer the dummy stack frame to the target. */
411 std::vector
<stack_item
>::reverse_iterator iter
;
412 for (iter
= stack_items
.rbegin (); iter
!= stack_items
.rend (); ++iter
)
415 write_memory (sp
, iter
->data
, iter
->len
);
418 /* Finally, update the SP register. */
419 regcache_cooked_write_unsigned (regcache
, CSKY_SP_REGNUM
, sp
);
423 /* Implement the return_value gdbarch method. */
425 static enum return_value_convention
426 csky_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
427 struct type
*valtype
, struct regcache
*regcache
,
428 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
431 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
432 int len
= TYPE_LENGTH (valtype
);
433 unsigned int ret_regnum
= CSKY_RET_REGNUM
;
435 /* Csky abi specifies that return values larger than 8 bytes
436 are put on the stack. */
438 return RETURN_VALUE_STRUCT_CONVENTION
;
444 /* By using store_unsigned_integer we avoid having to do
445 anything special for small big-endian values. */
446 regcache
->cooked_read (ret_regnum
, &tmp
);
447 store_unsigned_integer (readbuf
, (len
> 4 ? 4 : len
),
451 regcache
->cooked_read (ret_regnum
+ 1, &tmp
);
452 store_unsigned_integer (readbuf
+ 4, 4, byte_order
, tmp
);
455 if (writebuf
!= NULL
)
457 regval
= extract_unsigned_integer (writebuf
, len
> 4 ? 4 : len
,
459 regcache_cooked_write_unsigned (regcache
, ret_regnum
, regval
);
462 regval
= extract_unsigned_integer ((gdb_byte
*) writebuf
+ 4,
464 regcache_cooked_write_unsigned (regcache
, ret_regnum
+ 1,
469 return RETURN_VALUE_REGISTER_CONVENTION
;
473 /* Implement the frame_align gdbarch method.
475 Adjust the address downward (direction of stack growth) so that it
476 is correctly aligned for a new stack frame. */
479 csky_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
481 return align_down (addr
, 4);
484 /* Unwind cache used for gdbarch fallback unwinder. */
486 struct csky_unwind_cache
488 /* The stack pointer at the time this frame was created; i.e. the
489 caller's stack pointer when this function was called. It is used
490 to identify this frame. */
493 /* The frame base for this frame is just prev_sp - frame size.
494 FRAMESIZE is the distance from the frame pointer to the
495 initial stack pointer. */
498 /* The register used to hold the frame pointer for this frame. */
501 /* Saved register offsets. */
502 trad_frame_saved_reg
*saved_regs
;
505 /* Do prologue analysis, returning the PC of the first instruction
506 after the function prologue. */
509 csky_analyze_prologue (struct gdbarch
*gdbarch
,
513 struct frame_info
*this_frame
,
514 struct csky_unwind_cache
*this_cache
,
518 unsigned int insn
, rn
;
521 int register_offsets
[CSKY_NUM_GREGS_SAVED_GREGS
];
523 /* For adjusting fp. */
527 /* REGISTER_OFFSETS will contain offsets from the top of the frame
528 (NOT the frame pointer) for the various saved registers, or -1
529 if the register is not saved. */
530 for (rn
= 0; rn
< CSKY_NUM_GREGS_SAVED_GREGS
; rn
++)
531 register_offsets
[rn
] = -1;
533 /* Analyze the prologue. Things we determine from analyzing the
534 prologue include the size of the frame and which registers are
535 saved (and where). */
538 fprintf_unfiltered (gdb_stdlog
,
539 "csky: Scanning prologue: start_pc = 0x%x,"
540 "limit_pc = 0x%x\n", (unsigned int) start_pc
,
541 (unsigned int) limit_pc
);
544 /* Default to 16 bit instruction. */
547 for (addr
= start_pc
; addr
< limit_pc
; addr
+= insn_len
)
550 insn_len
= csky_get_insn (gdbarch
, addr
, &insn
);
552 /* Check if 32 bit. */
555 /* subi32 sp,sp oimm12. */
556 if (CSKY_32_IS_SUBI0 (insn
))
559 int offset
= CSKY_32_SUBI_IMM (insn
);
562 fprintf_unfiltered (gdb_stdlog
,
563 "csky: got subi sp,%d; continuing\n",
569 /* stm32 ry-rz,(sp). */
570 else if (CSKY_32_IS_STMx0 (insn
))
572 /* Spill register(s). */
577 /* BIG WARNING! The CKCore ABI does not restrict functions
578 to taking only one stack allocation. Therefore, when
579 we save a register, we record the offset of where it was
580 saved relative to the current stacksize. This will
581 then give an offset from the SP upon entry to our
582 function. Remember, stacksize is NOT constant until
583 we're done scanning the prologue. */
584 start_register
= CSKY_32_STM_VAL_REGNUM (insn
);
585 reg_count
= CSKY_32_STM_SIZE (insn
);
588 fprintf_unfiltered (gdb_stdlog
,
589 "csky: got stm r%d-r%d,(sp)\n",
591 start_register
+ reg_count
);
594 for (rn
= start_register
, offset
= 0;
595 rn
<= start_register
+ reg_count
;
598 register_offsets
[rn
] = stacksize
- offset
;
601 fprintf_unfiltered (gdb_stdlog
,
602 "csky: r%d saved at 0x%x"
604 rn
, register_offsets
[rn
],
609 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
612 /* stw ry,(sp,disp). */
613 else if (CSKY_32_IS_STWx0 (insn
))
615 /* Spill register: see note for IS_STM above. */
618 rn
= CSKY_32_ST_VAL_REGNUM (insn
);
619 disp
= CSKY_32_ST_OFFSET (insn
);
620 register_offsets
[rn
] = stacksize
- disp
;
622 print_savedreg_msg (rn
, register_offsets
, true);
625 else if (CSKY_32_IS_MOV_FP_SP (insn
))
627 /* SP is saved to FP reg, means code afer prologue may
630 adjust_fp
= stacksize
;
633 else if (CSKY_32_IS_MFCR_EPSR (insn
))
637 int mfcr_regnum
= insn
& 0x1f;
638 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
641 int stw_regnum
= (insn2
>> 5) & 0x7;
642 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
646 /* CSKY_EPSR_REGNUM. */
648 offset
= CSKY_16_STWx0_OFFSET (insn2
);
649 register_offsets
[rn
] = stacksize
- offset
;
651 print_savedreg_msg (rn
, register_offsets
, true);
659 int stw_regnum
= (insn2
>> 21) & 0x1f;
660 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
664 /* CSKY_EPSR_REGNUM. */
666 offset
= CSKY_32_ST_OFFSET (insn2
);
667 register_offsets
[rn
] = framesize
- offset
;
669 print_savedreg_msg (rn
, register_offsets
, true);
675 else if (CSKY_32_IS_MFCR_FPSR (insn
))
679 int mfcr_regnum
= insn
& 0x1f;
680 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
683 int stw_regnum
= (insn2
>> 5) & 0x7;
684 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
689 /* CSKY_FPSR_REGNUM. */
690 rn
= CSKY_NUM_GREGS
+ 1;
691 offset
= CSKY_16_STWx0_OFFSET (insn2
);
692 register_offsets
[rn
] = stacksize
- offset
;
694 print_savedreg_msg (rn
, register_offsets
, true);
702 int stw_regnum
= (insn2
>> 21) & 0x1f;
703 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
707 /* CSKY_FPSR_REGNUM. */
708 rn
= CSKY_NUM_GREGS
+ 1;
709 offset
= CSKY_32_ST_OFFSET (insn2
);
710 register_offsets
[rn
] = framesize
- offset
;
712 print_savedreg_msg (rn
, register_offsets
, true);
718 else if (CSKY_32_IS_MFCR_EPC (insn
))
722 int mfcr_regnum
= insn
& 0x1f;
723 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
726 int stw_regnum
= (insn2
>> 5) & 0x7;
727 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
731 /* CSKY_EPC_REGNUM. */
732 rn
= CSKY_NUM_GREGS
+ 2;
733 offset
= CSKY_16_STWx0_OFFSET (insn2
);
734 register_offsets
[rn
] = stacksize
- offset
;
736 print_savedreg_msg (rn
, register_offsets
, true);
744 int stw_regnum
= (insn2
>> 21) & 0x1f;
745 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
749 /* CSKY_EPC_REGNUM. */
750 rn
= CSKY_NUM_GREGS
+ 2;
751 offset
= CSKY_32_ST_OFFSET (insn2
);
752 register_offsets
[rn
] = framesize
- offset
;
754 print_savedreg_msg (rn
, register_offsets
, true);
760 else if (CSKY_32_IS_MFCR_FPC (insn
))
764 int mfcr_regnum
= insn
& 0x1f;
765 insn_len
= csky_get_insn (gdbarch
, addr
, &insn2
);
768 int stw_regnum
= (insn2
>> 5) & 0x7;
769 if (CSKY_16_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
773 /* CSKY_FPC_REGNUM. */
774 rn
= CSKY_NUM_GREGS
+ 3;
775 offset
= CSKY_16_STWx0_OFFSET (insn2
);
776 register_offsets
[rn
] = stacksize
- offset
;
778 print_savedreg_msg (rn
, register_offsets
, true);
786 int stw_regnum
= (insn2
>> 21) & 0x1f;
787 if (CSKY_32_IS_STWx0 (insn2
) && (mfcr_regnum
== stw_regnum
))
791 /* CSKY_FPC_REGNUM. */
792 rn
= CSKY_NUM_GREGS
+ 3;
793 offset
= CSKY_32_ST_OFFSET (insn2
);
794 register_offsets
[rn
] = framesize
- offset
;
796 print_savedreg_msg (rn
, register_offsets
, true);
802 else if (CSKY_32_IS_PUSH (insn
))
804 /* Push for 32_bit. */
806 if (CSKY_32_IS_PUSH_R29 (insn
))
809 register_offsets
[29] = stacksize
;
811 print_savedreg_msg (29, register_offsets
, false);
814 if (CSKY_32_PUSH_LIST2 (insn
))
816 int num
= CSKY_32_PUSH_LIST2 (insn
);
818 stacksize
+= num
* 4;
822 fprintf_unfiltered (gdb_stdlog
,
823 "csky: push regs_array: r16-r%d\n",
826 for (rn
= 16; rn
<= 16 + num
- 1; rn
++)
828 register_offsets
[rn
] = stacksize
- tmp
;
831 fprintf_unfiltered (gdb_stdlog
,
832 "csky: r%d saved at 0x%x"
833 " (offset %d)\n", rn
,
834 register_offsets
[rn
], tmp
);
839 if (CSKY_32_IS_PUSH_R15 (insn
))
842 register_offsets
[15] = stacksize
;
844 print_savedreg_msg (15, register_offsets
, false);
847 if (CSKY_32_PUSH_LIST1 (insn
))
849 int num
= CSKY_32_PUSH_LIST1 (insn
);
851 stacksize
+= num
* 4;
855 fprintf_unfiltered (gdb_stdlog
,
856 "csky: push regs_array: r4-r%d\n",
859 for (rn
= 4; rn
<= 4 + num
- 1; rn
++)
861 register_offsets
[rn
] = stacksize
- tmp
;
864 fprintf_unfiltered (gdb_stdlog
,
865 "csky: r%d saved at 0x%x"
866 " (offset %d)\n", rn
,
867 register_offsets
[rn
], tmp
);
873 framesize
= stacksize
;
875 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
878 else if (CSKY_32_IS_LRW4 (insn
) || CSKY_32_IS_MOVI4 (insn
)
879 || CSKY_32_IS_MOVIH4 (insn
) || CSKY_32_IS_BMASKI4 (insn
))
887 fprintf_unfiltered (gdb_stdlog
,
888 "csky: looking at large frame\n");
890 if (CSKY_32_IS_LRW4 (insn
))
892 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
893 int literal_addr
= (addr
+ ((insn
& 0xffff) << 2))
895 adjust
= read_memory_unsigned_integer (literal_addr
, 4,
898 else if (CSKY_32_IS_MOVI4 (insn
))
899 adjust
= (insn
& 0xffff);
900 else if (CSKY_32_IS_MOVIH4 (insn
))
901 adjust
= (insn
& 0xffff) << 16;
904 /* CSKY_32_IS_BMASKI4 (insn). */
905 adjust
= (1 << (((insn
& 0x3e00000) >> 21) + 1)) - 1;
910 fprintf_unfiltered (gdb_stdlog
,
911 "csky: base stacksize=0x%x\n", adjust
);
913 /* May have zero or more insns which modify r4. */
914 fprintf_unfiltered (gdb_stdlog
,
915 "csky: looking for r4 adjusters...\n");
919 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
920 while (CSKY_IS_R4_ADJUSTER (insn2
))
922 if (CSKY_32_IS_ADDI4 (insn2
))
924 int imm
= (insn2
& 0xfff) + 1;
928 fprintf_unfiltered (gdb_stdlog
,
929 "csky: addi r4,%d\n", imm
);
932 else if (CSKY_32_IS_SUBI4 (insn2
))
934 int imm
= (insn2
& 0xfff) + 1;
938 fprintf_unfiltered (gdb_stdlog
,
939 "csky: subi r4,%d\n", imm
);
942 else if (CSKY_32_IS_NOR4 (insn2
))
947 fprintf_unfiltered (gdb_stdlog
,
948 "csky: nor r4,r4,r4\n");
951 else if (CSKY_32_IS_ROTLI4 (insn2
))
953 int imm
= ((insn2
>> 21) & 0x1f);
954 int temp
= adjust
>> (32 - imm
);
959 fprintf_unfiltered (gdb_stdlog
,
960 "csky: rotli r4,r4,%d\n", imm
);
963 else if (CSKY_32_IS_LISI4 (insn2
))
965 int imm
= ((insn2
>> 21) & 0x1f);
969 fprintf_unfiltered (gdb_stdlog
,
970 "csky: lsli r4,r4,%d\n", imm
);
973 else if (CSKY_32_IS_BSETI4 (insn2
))
975 int imm
= ((insn2
>> 21) & 0x1f);
976 adjust
|= (1 << imm
);
979 fprintf_unfiltered (gdb_stdlog
,
980 "csky: bseti r4,r4 %d\n", imm
);
983 else if (CSKY_32_IS_BCLRI4 (insn2
))
985 int imm
= ((insn2
>> 21) & 0x1f);
986 adjust
&= ~(1 << imm
);
989 fprintf_unfiltered (gdb_stdlog
,
990 "csky: bclri r4,r4 %d\n", imm
);
993 else if (CSKY_32_IS_IXH4 (insn2
))
998 fprintf_unfiltered (gdb_stdlog
,
999 "csky: ixh r4,r4,r4\n");
1002 else if (CSKY_32_IS_IXW4 (insn2
))
1007 fprintf_unfiltered (gdb_stdlog
,
1008 "csky: ixw r4,r4,r4\n");
1011 else if (CSKY_16_IS_ADDI4 (insn2
))
1013 int imm
= (insn2
& 0xff) + 1;
1017 fprintf_unfiltered (gdb_stdlog
,
1018 "csky: addi r4,%d\n", imm
);
1021 else if (CSKY_16_IS_SUBI4 (insn2
))
1023 int imm
= (insn2
& 0xff) + 1;
1027 fprintf_unfiltered (gdb_stdlog
,
1028 "csky: subi r4,%d\n", imm
);
1031 else if (CSKY_16_IS_NOR4 (insn2
))
1036 fprintf_unfiltered (gdb_stdlog
,
1037 "csky: nor r4,r4\n");
1040 else if (CSKY_16_IS_BSETI4 (insn2
))
1042 int imm
= (insn2
& 0x1f);
1043 adjust
|= (1 << imm
);
1046 fprintf_unfiltered (gdb_stdlog
,
1047 "csky: bseti r4, %d\n", imm
);
1050 else if (CSKY_16_IS_BCLRI4 (insn2
))
1052 int imm
= (insn2
& 0x1f);
1053 adjust
&= ~(1 << imm
);
1056 fprintf_unfiltered (gdb_stdlog
,
1057 "csky: bclri r4, %d\n", imm
);
1060 else if (CSKY_16_IS_LSLI4 (insn2
))
1062 int imm
= (insn2
& 0x1f);
1066 fprintf_unfiltered (gdb_stdlog
,
1067 "csky: lsli r4,r4, %d\n", imm
);
1072 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
1077 fprintf_unfiltered (gdb_stdlog
, "csky: done looking for"
1081 /* If the next insn adjusts the stack pointer, we keep
1082 everything; if not, we scrap it and we've found the
1083 end of the prologue. */
1084 if (CSKY_IS_SUBU4 (insn2
))
1087 stacksize
+= adjust
;
1090 fprintf_unfiltered (gdb_stdlog
,
1091 "csky: found stack adjustment of"
1092 " 0x%x bytes.\n", adjust
);
1093 fprintf_unfiltered (gdb_stdlog
,
1094 "csky: skipping to new address %s\n",
1095 core_addr_to_string_nz (addr
));
1096 fprintf_unfiltered (gdb_stdlog
,
1097 "csky: continuing\n");
1102 /* None of these instructions are prologue, so don't touch
1106 fprintf_unfiltered (gdb_stdlog
,
1107 "csky: no subu sp,sp,r4; NOT altering"
1115 /* insn_len != 4. */
1117 /* subi.sp sp,disp. */
1118 if (CSKY_16_IS_SUBI0 (insn
))
1120 int offset
= CSKY_16_SUBI_IMM (insn
);
1123 fprintf_unfiltered (gdb_stdlog
,
1124 "csky: got subi r0,%d; continuing\n",
1127 stacksize
+= offset
;
1130 /* stw.16 rz,(sp,disp). */
1131 else if (CSKY_16_IS_STWx0 (insn
))
1133 /* Spill register: see note for IS_STM above. */
1136 rn
= CSKY_16_ST_VAL_REGNUM (insn
);
1137 disp
= CSKY_16_ST_OFFSET (insn
);
1138 register_offsets
[rn
] = stacksize
- disp
;
1140 print_savedreg_msg (rn
, register_offsets
, true);
1143 else if (CSKY_16_IS_MOV_FP_SP (insn
))
1145 /* SP is saved to FP reg, means prologue may modify SP. */
1147 adjust_fp
= stacksize
;
1150 else if (CSKY_16_IS_PUSH (insn
))
1152 /* Push for 16_bit. */
1154 if (CSKY_16_IS_PUSH_R15 (insn
))
1157 register_offsets
[15] = stacksize
;
1159 print_savedreg_msg (15, register_offsets
, false);
1162 if (CSKY_16_PUSH_LIST1 (insn
))
1164 int num
= CSKY_16_PUSH_LIST1 (insn
);
1166 stacksize
+= num
* 4;
1170 fprintf_unfiltered (gdb_stdlog
,
1171 "csky: push regs_array: r4-r%d\n",
1174 for (rn
= 4; rn
<= 4 + num
- 1; rn
++)
1176 register_offsets
[rn
] = stacksize
- tmp
;
1179 fprintf_unfiltered (gdb_stdlog
,
1180 "csky: r%d saved at 0x%x"
1181 " (offset %d)\n", rn
,
1182 register_offsets
[rn
], offset
);
1188 framesize
= stacksize
;
1190 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
1193 else if (CSKY_16_IS_LRW4 (insn
) || CSKY_16_IS_MOVI4 (insn
))
1200 fprintf_unfiltered (gdb_stdlog
,
1201 "csky: looking at large frame\n");
1203 if (CSKY_16_IS_LRW4 (insn
))
1205 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1206 int offset
= ((insn
& 0x300) >> 3) | (insn
& 0x1f);
1207 int literal_addr
= (addr
+ ( offset
<< 2)) & 0xfffffffc;
1208 adjust
= read_memory_unsigned_integer (literal_addr
, 4,
1213 /* CSKY_16_IS_MOVI4 (insn). */
1214 adjust
= (insn
& 0xff);
1219 fprintf_unfiltered (gdb_stdlog
,
1220 "csky: base stacksize=0x%x\n", adjust
);
1223 /* May have zero or more instructions which modify r4. */
1226 fprintf_unfiltered (gdb_stdlog
,
1227 "csky: looking for r4 adjusters...\n");
1230 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
1231 while (CSKY_IS_R4_ADJUSTER (insn2
))
1233 if (CSKY_32_IS_ADDI4 (insn2
))
1235 int imm
= (insn2
& 0xfff) + 1;
1239 fprintf_unfiltered (gdb_stdlog
,
1240 "csky: addi r4,%d\n", imm
);
1243 else if (CSKY_32_IS_SUBI4 (insn2
))
1245 int imm
= (insn2
& 0xfff) + 1;
1249 fprintf_unfiltered (gdb_stdlog
,
1250 "csky: subi r4,%d\n", imm
);
1253 else if (CSKY_32_IS_NOR4 (insn2
))
1258 fprintf_unfiltered (gdb_stdlog
,
1259 "csky: nor r4,r4,r4\n");
1262 else if (CSKY_32_IS_ROTLI4 (insn2
))
1264 int imm
= ((insn2
>> 21) & 0x1f);
1265 int temp
= adjust
>> (32 - imm
);
1270 fprintf_unfiltered (gdb_stdlog
,
1271 "csky: rotli r4,r4,%d\n", imm
);
1274 else if (CSKY_32_IS_LISI4 (insn2
))
1276 int imm
= ((insn2
>> 21) & 0x1f);
1280 fprintf_unfiltered (gdb_stdlog
,
1281 "csky: lsli r4,r4,%d\n", imm
);
1284 else if (CSKY_32_IS_BSETI4 (insn2
))
1286 int imm
= ((insn2
>> 21) & 0x1f);
1287 adjust
|= (1 << imm
);
1290 fprintf_unfiltered (gdb_stdlog
,
1291 "csky: bseti r4,r4 %d\n", imm
);
1294 else if (CSKY_32_IS_BCLRI4 (insn2
))
1296 int imm
= ((insn2
>> 21) & 0x1f);
1297 adjust
&= ~(1 << imm
);
1300 fprintf_unfiltered (gdb_stdlog
,
1301 "csky: bclri r4,r4 %d\n", imm
);
1304 else if (CSKY_32_IS_IXH4 (insn2
))
1309 fprintf_unfiltered (gdb_stdlog
,
1310 "csky: ixh r4,r4,r4\n");
1313 else if (CSKY_32_IS_IXW4 (insn2
))
1318 fprintf_unfiltered (gdb_stdlog
,
1319 "csky: ixw r4,r4,r4\n");
1322 else if (CSKY_16_IS_ADDI4 (insn2
))
1324 int imm
= (insn2
& 0xff) + 1;
1328 fprintf_unfiltered (gdb_stdlog
,
1329 "csky: addi r4,%d\n", imm
);
1332 else if (CSKY_16_IS_SUBI4 (insn2
))
1334 int imm
= (insn2
& 0xff) + 1;
1338 fprintf_unfiltered (gdb_stdlog
,
1339 "csky: subi r4,%d\n", imm
);
1342 else if (CSKY_16_IS_NOR4 (insn2
))
1347 fprintf_unfiltered (gdb_stdlog
,
1348 "csky: nor r4,r4\n");
1351 else if (CSKY_16_IS_BSETI4 (insn2
))
1353 int imm
= (insn2
& 0x1f);
1354 adjust
|= (1 << imm
);
1357 fprintf_unfiltered (gdb_stdlog
,
1358 "csky: bseti r4, %d\n", imm
);
1361 else if (CSKY_16_IS_BCLRI4 (insn2
))
1363 int imm
= (insn2
& 0x1f);
1364 adjust
&= ~(1 << imm
);
1367 fprintf_unfiltered (gdb_stdlog
,
1368 "csky: bclri r4, %d\n", imm
);
1371 else if (CSKY_16_IS_LSLI4 (insn2
))
1373 int imm
= (insn2
& 0x1f);
1377 fprintf_unfiltered (gdb_stdlog
,
1378 "csky: lsli r4,r4, %d\n", imm
);
1383 insn_len
= csky_get_insn (gdbarch
, addr
+ offset
, &insn2
);
1388 fprintf_unfiltered (gdb_stdlog
, "csky: "
1389 "done looking for r4 adjusters\n");
1392 /* If the next instruction adjusts the stack pointer, we keep
1393 everything; if not, we scrap it and we've found the end
1395 if (CSKY_IS_SUBU4 (insn2
))
1398 stacksize
+= adjust
;
1401 fprintf_unfiltered (gdb_stdlog
, "csky: "
1402 "found stack adjustment of 0x%x"
1403 " bytes.\n", adjust
);
1404 fprintf_unfiltered (gdb_stdlog
, "csky: "
1405 "skipping to new address %s\n",
1406 core_addr_to_string_nz (addr
));
1407 fprintf_unfiltered (gdb_stdlog
, "csky: continuing\n");
1412 /* None of these instructions are prologue, so don't touch
1416 fprintf_unfiltered (gdb_stdlog
, "csky: no subu sp,r4; "
1417 "NOT altering stacksize.\n");
1423 /* This is not a prologue instruction, so stop here. */
1426 fprintf_unfiltered (gdb_stdlog
, "csky: insn is not a prologue"
1427 " insn -- ending scan\n");
1434 CORE_ADDR unwound_fp
;
1435 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1436 this_cache
->framesize
= framesize
;
1440 this_cache
->framereg
= CSKY_FP_REGNUM
;
1441 unwound_fp
= get_frame_register_unsigned (this_frame
,
1442 this_cache
->framereg
);
1443 this_cache
->prev_sp
= unwound_fp
+ adjust_fp
;
1447 this_cache
->framereg
= CSKY_SP_REGNUM
;
1448 unwound_fp
= get_frame_register_unsigned (this_frame
,
1449 this_cache
->framereg
);
1450 this_cache
->prev_sp
= unwound_fp
+ stacksize
;
1453 /* Note where saved registers are stored. The offsets in
1454 REGISTER_OFFSETS are computed relative to the top of the frame. */
1455 for (rn
= 0; rn
< CSKY_NUM_GREGS
; rn
++)
1457 if (register_offsets
[rn
] >= 0)
1459 this_cache
->saved_regs
[rn
].set_addr (this_cache
->prev_sp
1460 - register_offsets
[rn
]);
1463 CORE_ADDR rn_value
= read_memory_unsigned_integer (
1464 this_cache
->saved_regs
[rn
].addr (), 4, byte_order
);
1465 fprintf_unfiltered (gdb_stdlog
, "Saved register %s "
1466 "stored at 0x%08lx, value=0x%08lx\n",
1467 csky_register_names
[rn
],
1469 this_cache
->saved_regs
[rn
].addr (),
1470 (unsigned long) rn_value
);
1474 if (lr_type
== LR_TYPE_EPC
)
1477 this_cache
->saved_regs
[CSKY_PC_REGNUM
]
1478 = this_cache
->saved_regs
[CSKY_EPC_REGNUM
];
1480 else if (lr_type
== LR_TYPE_FPC
)
1483 this_cache
->saved_regs
[CSKY_PC_REGNUM
]
1484 = this_cache
->saved_regs
[CSKY_FPC_REGNUM
];
1488 this_cache
->saved_regs
[CSKY_PC_REGNUM
]
1489 = this_cache
->saved_regs
[CSKY_LR_REGNUM
];
1496 /* Detect whether PC is at a point where the stack frame has been
1500 csky_stack_frame_destroyed_p (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1504 CORE_ADDR func_start
, func_end
;
1506 if (!find_pc_partial_function (pc
, NULL
, &func_start
, &func_end
))
1509 bool fp_saved
= false;
1511 for (addr
= func_start
; addr
< func_end
; addr
+= insn_len
)
1513 /* Get next insn. */
1514 insn_len
= csky_get_insn (gdbarch
, addr
, &insn
);
1518 /* Is sp is saved to fp. */
1519 if (CSKY_16_IS_MOV_FP_SP (insn
))
1521 /* If sp was saved to fp and now being restored from
1522 fp then it indicates the start of epilog. */
1523 else if (fp_saved
&& CSKY_16_IS_MOV_SP_FP (insn
))
1530 /* Implement the skip_prologue gdbarch hook. */
1533 csky_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1535 CORE_ADDR func_addr
, func_end
;
1536 struct symtab_and_line sal
;
1537 const int default_search_limit
= 128;
1539 /* See if we can find the end of the prologue using the symbol table. */
1540 if (find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
1542 CORE_ADDR post_prologue_pc
1543 = skip_prologue_using_sal (gdbarch
, func_addr
);
1545 if (post_prologue_pc
!= 0)
1546 return std::max (pc
, post_prologue_pc
);
1549 func_end
= pc
+ default_search_limit
;
1551 /* Find the end of prologue. Default lr_type. */
1552 return csky_analyze_prologue (gdbarch
, pc
, func_end
, func_end
,
1553 NULL
, NULL
, LR_TYPE_R15
);
1556 /* Implement the breakpoint_kind_from_pc gdbarch method. */
1559 csky_breakpoint_kind_from_pc (struct gdbarch
*gdbarch
, CORE_ADDR
*pcptr
)
1561 if (csky_pc_is_csky16 (gdbarch
, *pcptr
))
1562 return CSKY_INSN_SIZE16
;
1564 return CSKY_INSN_SIZE32
;
1567 /* Implement the sw_breakpoint_from_kind gdbarch method. */
1569 static const gdb_byte
*
1570 csky_sw_breakpoint_from_kind (struct gdbarch
*gdbarch
, int kind
, int *size
)
1573 if (kind
== CSKY_INSN_SIZE16
)
1575 static gdb_byte csky_16_breakpoint
[] = { 0, 0 };
1576 return csky_16_breakpoint
;
1580 static gdb_byte csky_32_breakpoint
[] = { 0, 0, 0, 0 };
1581 return csky_32_breakpoint
;
1585 /* Implement the memory_insert_breakpoint gdbarch method. */
1588 csky_memory_insert_breakpoint (struct gdbarch
*gdbarch
,
1589 struct bp_target_info
*bp_tgt
)
1592 const unsigned char *bp
;
1593 gdb_byte bp_write_record1
[] = { 0, 0, 0, 0 };
1594 gdb_byte bp_write_record2
[] = { 0, 0, 0, 0 };
1595 gdb_byte bp_record
[] = { 0, 0, 0, 0 };
1597 /* Sanity-check bp_address. */
1598 if (bp_tgt
->reqstd_address
% 2)
1599 warning (_("Invalid breakpoint address 0x%x is an odd number."),
1600 (unsigned int) bp_tgt
->reqstd_address
);
1601 scoped_restore restore_memory
1602 = make_scoped_restore_show_memory_breakpoints (1);
1604 /* Determine appropriate breakpoint_kind for this address. */
1605 bp_tgt
->kind
= csky_breakpoint_kind_from_pc (gdbarch
,
1606 &bp_tgt
->reqstd_address
);
1608 /* Save the memory contents. */
1609 bp_tgt
->shadow_len
= bp_tgt
->kind
;
1611 /* Fill bp_tgt->placed_address. */
1612 bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
1614 if (bp_tgt
->kind
== CSKY_INSN_SIZE16
)
1616 if ((bp_tgt
->reqstd_address
% 4) == 0)
1618 /* Read two bytes. */
1619 val
= target_read_memory (bp_tgt
->reqstd_address
,
1620 bp_tgt
->shadow_contents
, 2);
1624 /* Read two bytes. */
1625 val
= target_read_memory (bp_tgt
->reqstd_address
+ 2,
1630 /* Write the breakpoint. */
1631 bp_write_record1
[2] = bp_record
[0];
1632 bp_write_record1
[3] = bp_record
[1];
1633 bp
= bp_write_record1
;
1634 val
= target_write_raw_memory (bp_tgt
->reqstd_address
, bp
,
1639 val
= target_read_memory (bp_tgt
->reqstd_address
,
1640 bp_tgt
->shadow_contents
, 2);
1644 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1649 /* Write the breakpoint. */
1650 bp_write_record1
[0] = bp_record
[0];
1651 bp_write_record1
[1] = bp_record
[1];
1652 bp
= bp_write_record1
;
1653 val
= target_write_raw_memory (bp_tgt
->reqstd_address
- 2,
1654 bp
, CSKY_WR_BKPT_MODE
);
1659 if (bp_tgt
->placed_address
% 4 == 0)
1661 val
= target_read_memory (bp_tgt
->reqstd_address
,
1662 bp_tgt
->shadow_contents
,
1667 /* Write the breakpoint. */
1668 bp
= bp_write_record1
;
1669 val
= target_write_raw_memory (bp_tgt
->reqstd_address
,
1670 bp
, CSKY_WR_BKPT_MODE
);
1674 val
= target_read_memory (bp_tgt
->reqstd_address
,
1675 bp_tgt
->shadow_contents
,
1680 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1685 val
= target_read_memory (bp_tgt
->reqstd_address
+ 4,
1690 bp_write_record1
[0] = bp_record
[0];
1691 bp_write_record1
[1] = bp_record
[1];
1692 bp_write_record2
[2] = bp_record
[2];
1693 bp_write_record2
[3] = bp_record
[3];
1695 /* Write the breakpoint. */
1696 bp
= bp_write_record1
;
1697 val
= target_write_raw_memory (bp_tgt
->reqstd_address
- 2, bp
,
1702 /* Write the breakpoint. */
1703 bp
= bp_write_record2
;
1704 val
= target_write_raw_memory (bp_tgt
->reqstd_address
+ 2, bp
,
1711 /* Restore the breakpoint shadow_contents to the target. */
1714 csky_memory_remove_breakpoint (struct gdbarch
*gdbarch
,
1715 struct bp_target_info
*bp_tgt
)
1718 gdb_byte bp_record
[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1719 /* Different for shadow_len 2 or 4. */
1720 if (bp_tgt
->shadow_len
== 2)
1722 /* Do word-sized writes on word-aligned boundaries and read
1723 padding bytes as necessary. */
1724 if (bp_tgt
->reqstd_address
% 4 == 0)
1726 val
= target_read_memory (bp_tgt
->reqstd_address
+ 2,
1730 bp_record
[0] = bp_tgt
->shadow_contents
[0];
1731 bp_record
[1] = bp_tgt
->shadow_contents
[1];
1732 return target_write_raw_memory (bp_tgt
->reqstd_address
,
1733 bp_record
, CSKY_WR_BKPT_MODE
);
1737 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1741 bp_record
[2] = bp_tgt
->shadow_contents
[0];
1742 bp_record
[3] = bp_tgt
->shadow_contents
[1];
1743 return target_write_raw_memory (bp_tgt
->reqstd_address
- 2,
1744 bp_record
, CSKY_WR_BKPT_MODE
);
1749 /* Do word-sized writes on word-aligned boundaries and read
1750 padding bytes as necessary. */
1751 if (bp_tgt
->placed_address
% 4 == 0)
1753 return target_write_raw_memory (bp_tgt
->reqstd_address
,
1754 bp_tgt
->shadow_contents
,
1759 val
= target_read_memory (bp_tgt
->reqstd_address
- 2,
1763 val
= target_read_memory (bp_tgt
->reqstd_address
+ 4,
1768 bp_record
[2] = bp_tgt
->shadow_contents
[0];
1769 bp_record
[3] = bp_tgt
->shadow_contents
[1];
1770 bp_record
[4] = bp_tgt
->shadow_contents
[2];
1771 bp_record
[5] = bp_tgt
->shadow_contents
[3];
1773 return target_write_raw_memory (bp_tgt
->reqstd_address
- 2,
1775 CSKY_WR_BKPT_MODE
* 2);
1780 /* Determine link register type. */
1783 csky_analyze_lr_type (struct gdbarch
*gdbarch
,
1784 CORE_ADDR start_pc
, CORE_ADDR end_pc
)
1787 unsigned int insn
, insn_len
;
1790 for (addr
= start_pc
; addr
< end_pc
; addr
+= insn_len
)
1792 insn_len
= csky_get_insn (gdbarch
, addr
, &insn
);
1795 if (CSKY_32_IS_MFCR_EPSR (insn
) || CSKY_32_IS_MFCR_EPC (insn
)
1796 || CSKY_32_IS_RTE (insn
))
1799 else if (CSKY_32_IS_MFCR_FPSR (insn
) || CSKY_32_IS_MFCR_FPC (insn
)
1800 || CSKY_32_IS_RFI (insn
))
1802 else if (CSKY_32_IS_JMP (insn
) || CSKY_32_IS_BR (insn
)
1803 || CSKY_32_IS_JMPIX (insn
) || CSKY_32_IS_JMPI (insn
))
1807 /* 16 bit instruction. */
1808 if (CSKY_16_IS_JMP (insn
) || CSKY_16_IS_BR (insn
)
1809 || CSKY_16_IS_JMPIX (insn
))
1816 /* Heuristic unwinder. */
1818 static struct csky_unwind_cache
*
1819 csky_frame_unwind_cache (struct frame_info
*this_frame
)
1821 CORE_ADDR prologue_start
, prologue_end
, func_end
, prev_pc
, block_addr
;
1822 struct csky_unwind_cache
*cache
;
1823 const struct block
*bl
;
1824 unsigned long func_size
= 0;
1825 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1826 unsigned int sp_regnum
= CSKY_SP_REGNUM
;
1828 /* Default lr type is r15. */
1829 lr_type_t lr_type
= LR_TYPE_R15
;
1831 cache
= FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache
);
1832 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
1834 /* Assume there is no frame until proven otherwise. */
1835 cache
->framereg
= sp_regnum
;
1837 cache
->framesize
= 0;
1839 prev_pc
= get_frame_pc (this_frame
);
1840 block_addr
= get_frame_address_in_block (this_frame
);
1841 if (find_pc_partial_function (block_addr
, NULL
, &prologue_start
,
1843 /* We couldn't find a function containing block_addr, so bail out
1844 and hope for the best. */
1847 /* Get the (function) symbol matching prologue_start. */
1848 bl
= block_for_pc (prologue_start
);
1850 func_size
= bl
->endaddr
- bl
->startaddr
;
1853 struct bound_minimal_symbol msymbol
1854 = lookup_minimal_symbol_by_pc (prologue_start
);
1855 if (msymbol
.minsym
!= NULL
)
1856 func_size
= MSYMBOL_SIZE (msymbol
.minsym
);
1859 /* If FUNC_SIZE is 0 we may have a special-case use of lr
1860 e.g. exception or interrupt. */
1862 lr_type
= csky_analyze_lr_type (gdbarch
, prologue_start
, func_end
);
1864 prologue_end
= std::min (func_end
, prev_pc
);
1866 /* Analyze the function prologue. */
1867 csky_analyze_prologue (gdbarch
, prologue_start
, prologue_end
,
1868 func_end
, this_frame
, cache
, lr_type
);
1870 /* gdbarch_sp_regnum contains the value and not the address. */
1871 cache
->saved_regs
[sp_regnum
].set_value (cache
->prev_sp
);
1875 /* Implement the this_id function for the normal unwinder. */
1878 csky_frame_this_id (struct frame_info
*this_frame
,
1879 void **this_prologue_cache
, struct frame_id
*this_id
)
1881 struct csky_unwind_cache
*cache
;
1884 if (*this_prologue_cache
== NULL
)
1885 *this_prologue_cache
= csky_frame_unwind_cache (this_frame
);
1886 cache
= (struct csky_unwind_cache
*) *this_prologue_cache
;
1888 /* This marks the outermost frame. */
1889 if (cache
->prev_sp
== 0)
1892 id
= frame_id_build (cache
->prev_sp
, get_frame_func (this_frame
));
1896 /* Implement the prev_register function for the normal unwinder. */
1898 static struct value
*
1899 csky_frame_prev_register (struct frame_info
*this_frame
,
1900 void **this_prologue_cache
, int regnum
)
1902 struct csky_unwind_cache
*cache
;
1904 if (*this_prologue_cache
== NULL
)
1905 *this_prologue_cache
= csky_frame_unwind_cache (this_frame
);
1906 cache
= (struct csky_unwind_cache
*) *this_prologue_cache
;
1908 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
,
1912 /* Data structures for the normal prologue-analysis-based
1915 static const struct frame_unwind csky_unwind_cache
= {
1918 default_frame_unwind_stop_reason
,
1920 csky_frame_prev_register
,
1922 default_frame_sniffer
,
1930 csky_stub_unwind_sniffer (const struct frame_unwind
*self
,
1931 struct frame_info
*this_frame
,
1932 void **this_prologue_cache
)
1934 CORE_ADDR addr_in_block
;
1936 addr_in_block
= get_frame_address_in_block (this_frame
);
1938 if (find_pc_partial_function (addr_in_block
, NULL
, NULL
, NULL
) == 0
1939 || in_plt_section (addr_in_block
))
1945 static struct csky_unwind_cache
*
1946 csky_make_stub_cache (struct frame_info
*this_frame
)
1948 struct csky_unwind_cache
*cache
;
1950 cache
= FRAME_OBSTACK_ZALLOC (struct csky_unwind_cache
);
1951 cache
->saved_regs
= trad_frame_alloc_saved_regs (this_frame
);
1952 cache
->prev_sp
= get_frame_register_unsigned (this_frame
, CSKY_SP_REGNUM
);
1958 csky_stub_this_id (struct frame_info
*this_frame
,
1960 struct frame_id
*this_id
)
1962 struct csky_unwind_cache
*cache
;
1964 if (*this_cache
== NULL
)
1965 *this_cache
= csky_make_stub_cache (this_frame
);
1966 cache
= (struct csky_unwind_cache
*) *this_cache
;
1968 /* Our frame ID for a stub frame is the current SP and LR. */
1969 *this_id
= frame_id_build (cache
->prev_sp
, get_frame_pc (this_frame
));
1972 static struct value
*
1973 csky_stub_prev_register (struct frame_info
*this_frame
,
1977 struct csky_unwind_cache
*cache
;
1979 if (*this_cache
== NULL
)
1980 *this_cache
= csky_make_stub_cache (this_frame
);
1981 cache
= (struct csky_unwind_cache
*) *this_cache
;
1983 /* If we are asked to unwind the PC, then return the LR. */
1984 if (prev_regnum
== CSKY_PC_REGNUM
)
1988 lr
= frame_unwind_register_unsigned (this_frame
, CSKY_LR_REGNUM
);
1989 return frame_unwind_got_constant (this_frame
, prev_regnum
, lr
);
1992 if (prev_regnum
== CSKY_SP_REGNUM
)
1993 return frame_unwind_got_constant (this_frame
, prev_regnum
, cache
->prev_sp
);
1995 return trad_frame_get_prev_register (this_frame
, cache
->saved_regs
,
1999 static frame_unwind csky_stub_unwind
= {
2002 default_frame_unwind_stop_reason
,
2004 csky_stub_prev_register
,
2006 csky_stub_unwind_sniffer
2009 /* Implement the this_base, this_locals, and this_args hooks
2010 for the normal unwinder. */
2013 csky_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
2015 struct csky_unwind_cache
*cache
;
2017 if (*this_cache
== NULL
)
2018 *this_cache
= csky_frame_unwind_cache (this_frame
);
2019 cache
= (struct csky_unwind_cache
*) *this_cache
;
2021 return cache
->prev_sp
- cache
->framesize
;
2024 static const struct frame_base csky_frame_base
= {
2026 csky_frame_base_address
,
2027 csky_frame_base_address
,
2028 csky_frame_base_address
2031 /* Initialize register access method. */
2034 csky_dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
2035 struct dwarf2_frame_state_reg
*reg
,
2036 struct frame_info
*this_frame
)
2038 if (regnum
== gdbarch_pc_regnum (gdbarch
))
2039 reg
->how
= DWARF2_FRAME_REG_RA
;
2040 else if (regnum
== gdbarch_sp_regnum (gdbarch
))
2041 reg
->how
= DWARF2_FRAME_REG_CFA
;
2044 /* Create csky register groups. */
2047 csky_init_reggroup ()
2049 cr_reggroup
= reggroup_new ("cr", USER_REGGROUP
);
2050 fr_reggroup
= reggroup_new ("fr", USER_REGGROUP
);
2051 vr_reggroup
= reggroup_new ("vr", USER_REGGROUP
);
2052 mmu_reggroup
= reggroup_new ("mmu", USER_REGGROUP
);
2053 prof_reggroup
= reggroup_new ("profiling", USER_REGGROUP
);
2056 /* Add register groups into reggroup list. */
2059 csky_add_reggroups (struct gdbarch
*gdbarch
)
2061 reggroup_add (gdbarch
, all_reggroup
);
2062 reggroup_add (gdbarch
, general_reggroup
);
2063 reggroup_add (gdbarch
, cr_reggroup
);
2064 reggroup_add (gdbarch
, fr_reggroup
);
2065 reggroup_add (gdbarch
, vr_reggroup
);
2066 reggroup_add (gdbarch
, mmu_reggroup
);
2067 reggroup_add (gdbarch
, prof_reggroup
);
2070 /* Return the groups that a CSKY register can be categorised into. */
2073 csky_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
2074 struct reggroup
*reggroup
)
2078 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
2079 || gdbarch_register_name (gdbarch
, regnum
)[0] == '\0')
2082 if (reggroup
== all_reggroup
)
2085 raw_p
= regnum
< gdbarch_num_regs (gdbarch
);
2086 if (reggroup
== save_reggroup
|| reggroup
== restore_reggroup
)
2089 if (((regnum
>= CSKY_R0_REGNUM
) && (regnum
<= CSKY_R0_REGNUM
+ 31))
2090 && (reggroup
== general_reggroup
))
2093 if (((regnum
== CSKY_PC_REGNUM
)
2094 || ((regnum
>= CSKY_CR0_REGNUM
)
2095 && (regnum
<= CSKY_CR0_REGNUM
+ 30)))
2096 && (reggroup
== cr_reggroup
))
2099 if ((((regnum
>= CSKY_VR0_REGNUM
) && (regnum
<= CSKY_VR0_REGNUM
+ 15))
2100 || ((regnum
>= CSKY_VCR0_REGNUM
)
2101 && (regnum
<= CSKY_VCR0_REGNUM
+ 2)))
2102 && (reggroup
== vr_reggroup
))
2105 if (((regnum
>= CSKY_MMU_REGNUM
) && (regnum
<= CSKY_MMU_REGNUM
+ 8))
2106 && (reggroup
== mmu_reggroup
))
2109 if (((regnum
>= CSKY_PROFCR_REGNUM
)
2110 && (regnum
<= CSKY_PROFCR_REGNUM
+ 48))
2111 && (reggroup
== prof_reggroup
))
2114 if ((((regnum
>= CSKY_FR0_REGNUM
) && (regnum
<= CSKY_FR0_REGNUM
+ 15))
2115 || ((regnum
>= CSKY_VCR0_REGNUM
) && (regnum
<= CSKY_VCR0_REGNUM
+ 2)))
2116 && (reggroup
== fr_reggroup
))
2122 /* Implement the dwarf2_reg_to_regnum gdbarch method. */
2125 csky_dwarf_reg_to_regnum (struct gdbarch
*gdbarch
, int dw_reg
)
2127 if (dw_reg
< 0 || dw_reg
>= CSKY_NUM_REGS
)
2132 /* Override interface for command: info register. */
2135 csky_print_registers_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
2136 struct frame_info
*frame
, int regnum
, int all
)
2138 /* Call default print_registers_info function. */
2139 default_print_registers_info (gdbarch
, file
, frame
, regnum
, all
);
2141 /* For command: info register. */
2142 if (regnum
== -1 && all
== 0)
2144 default_print_registers_info (gdbarch
, file
, frame
,
2146 default_print_registers_info (gdbarch
, file
, frame
,
2147 CSKY_EPC_REGNUM
, 0);
2148 default_print_registers_info (gdbarch
, file
, frame
,
2149 CSKY_CR0_REGNUM
, 0);
2150 default_print_registers_info (gdbarch
, file
, frame
,
2151 CSKY_EPSR_REGNUM
, 0);
2156 /* Initialize the current architecture based on INFO. If possible,
2157 re-use an architecture from ARCHES, which is a list of
2158 architectures already created during this debugging session.
2160 Called at program startup, when reading a core file, and when
2161 reading a binary file. */
2163 static struct gdbarch
*
2164 csky_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
2166 struct gdbarch
*gdbarch
;
2168 /* Find a candidate among the list of pre-declared architectures. */
2169 arches
= gdbarch_list_lookup_by_info (arches
, &info
);
2171 return arches
->gdbarch
;
2173 /* None found, create a new architecture from the information
2175 csky_gdbarch_tdep
*tdep
= new csky_gdbarch_tdep
;
2176 gdbarch
= gdbarch_alloc (&info
, tdep
);
2178 /* Target data types. */
2179 set_gdbarch_ptr_bit (gdbarch
, 32);
2180 set_gdbarch_addr_bit (gdbarch
, 32);
2181 set_gdbarch_short_bit (gdbarch
, 16);
2182 set_gdbarch_int_bit (gdbarch
, 32);
2183 set_gdbarch_long_bit (gdbarch
, 32);
2184 set_gdbarch_long_long_bit (gdbarch
, 64);
2185 set_gdbarch_float_bit (gdbarch
, 32);
2186 set_gdbarch_double_bit (gdbarch
, 64);
2187 set_gdbarch_float_format (gdbarch
, floatformats_ieee_single
);
2188 set_gdbarch_double_format (gdbarch
, floatformats_ieee_double
);
2190 /* Information about the target architecture. */
2191 set_gdbarch_return_value (gdbarch
, csky_return_value
);
2192 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, csky_breakpoint_kind_from_pc
);
2193 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, csky_sw_breakpoint_from_kind
);
2195 /* Register architecture. */
2196 set_gdbarch_num_regs (gdbarch
, CSKY_NUM_REGS
);
2197 set_gdbarch_pc_regnum (gdbarch
, CSKY_PC_REGNUM
);
2198 set_gdbarch_sp_regnum (gdbarch
, CSKY_SP_REGNUM
);
2199 set_gdbarch_register_name (gdbarch
, csky_register_name
);
2200 set_gdbarch_register_type (gdbarch
, csky_register_type
);
2201 set_gdbarch_read_pc (gdbarch
, csky_read_pc
);
2202 set_gdbarch_write_pc (gdbarch
, csky_write_pc
);
2203 set_gdbarch_print_registers_info (gdbarch
, csky_print_registers_info
);
2204 csky_add_reggroups (gdbarch
);
2205 set_gdbarch_register_reggroup_p (gdbarch
, csky_register_reggroup_p
);
2206 set_gdbarch_stab_reg_to_regnum (gdbarch
, csky_dwarf_reg_to_regnum
);
2207 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, csky_dwarf_reg_to_regnum
);
2208 dwarf2_frame_set_init_reg (gdbarch
, csky_dwarf2_frame_init_reg
);
2210 /* Functions to analyze frames. */
2211 frame_base_set_default (gdbarch
, &csky_frame_base
);
2212 set_gdbarch_skip_prologue (gdbarch
, csky_skip_prologue
);
2213 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
2214 set_gdbarch_frame_align (gdbarch
, csky_frame_align
);
2215 set_gdbarch_stack_frame_destroyed_p (gdbarch
, csky_stack_frame_destroyed_p
);
2217 /* Functions handling dummy frames. */
2218 set_gdbarch_push_dummy_call (gdbarch
, csky_push_dummy_call
);
2220 /* Frame unwinders. Use DWARF debug info if available,
2221 otherwise use our own unwinder. */
2222 dwarf2_append_unwinders (gdbarch
);
2223 frame_unwind_append_unwinder (gdbarch
, &csky_stub_unwind
);
2224 frame_unwind_append_unwinder (gdbarch
, &csky_unwind_cache
);
2227 set_gdbarch_memory_insert_breakpoint (gdbarch
,
2228 csky_memory_insert_breakpoint
);
2229 set_gdbarch_memory_remove_breakpoint (gdbarch
,
2230 csky_memory_remove_breakpoint
);
2232 /* Hook in ABI-specific overrides, if they have been registered. */
2233 gdbarch_init_osabi (info
, gdbarch
);
2235 /* Support simple overlay manager. */
2236 set_gdbarch_overlay_update (gdbarch
, simple_overlay_update
);
2237 set_gdbarch_char_signed (gdbarch
, 0);
2241 void _initialize_csky_tdep ();
2243 _initialize_csky_tdep ()
2246 register_gdbarch_init (bfd_arch_csky
, csky_gdbarch_init
);
2248 csky_init_reggroup ();
2250 /* Allow debugging this file's internals. */
2251 add_setshow_boolean_cmd ("csky", class_maintenance
, &csky_debug
,
2252 _("Set C-Sky debugging."),
2253 _("Show C-Sky debugging."),
2254 _("When on, C-Sky specific debugging is enabled."),
2257 &setdebuglist
, &showdebuglist
);