Automatic date update in version.in
[binutils-gdb.git] / gdb / csky-linux-tdep.c
bloba703b389d2eaf156799d44923d655ee17a41fff6
1 /* Target-dependent code for GNU/Linux on CSKY.
3 Copyright (C) 2012-2024 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/>. */
22 #include "osabi.h"
23 #include "glibc-tdep.h"
24 #include "linux-tdep.h"
25 #include "gdbarch.h"
26 #include "solib-svr4.h"
27 #include "regset.h"
28 #include "trad-frame.h"
29 #include "tramp-frame.h"
30 #include "csky-tdep.h"
32 /* Functions, definitions, and data structures for C-Sky core file debug. */
34 /* General regset pc, r1, r0, psr, r2-r31 for CK810. */
35 #define SIZEOF_CSKY_GREGSET 34*4
36 /* Float regset fesr fsr fr0-fr31 for CK810. */
37 #define SIZEOF_CSKY_FREGSET 34*4
38 /* Float regset vr0~vr15 fr15~fr31, reserved for CK810 when kernel 4.x. */
39 #define SIZEOF_CSKY_FREGSET_K4X 400
41 /* Offset mapping table from core_section to regcache of general
42 registers for ck810. */
43 static const int csky_gregset_offset[] =
45 72, 1, 0, 89, 2, /* pc, r1, r0, psr, r2. */
46 3, 4, 5, 6, 7, /* r3 ~ r32. */
47 8, 9, 10, 11, 12,
48 13, 14, 15, 16, 17,
49 18, 19, 20, 21, 22,
50 23, 24, 25, 26, 27,
51 28, 29, 30, 31
54 /* Offset mapping table from core_section to regcache of float
55 registers for ck810. */
57 static const int csky_fregset_offset[] =
59 122, 123, 40, 41, 42, /* fcr, fesr, fr0 ~ fr2. */
60 43, 44, 45, 46, 47, /* fr3 ~ fr15. */
61 48, 49, 50, 51, 52,
62 53, 54, 55
65 /* Implement the supply_regset hook for GP registers in core files. */
67 static void
68 csky_supply_gregset (const struct regset *regset,
69 struct regcache *regcache, int regnum,
70 const void *regs, size_t len)
72 int i, gregset_num;
73 const gdb_byte *gregs = (const gdb_byte *) regs ;
75 gdb_assert (len >= SIZEOF_CSKY_GREGSET);
76 gregset_num = ARRAY_SIZE (csky_gregset_offset);
78 for (i = 0; i < gregset_num; i++)
80 if ((regnum == csky_gregset_offset[i] || regnum == -1)
81 && csky_gregset_offset[i] != -1)
82 regcache->raw_supply (csky_gregset_offset[i], gregs + 4 * i);
86 /* Implement the collect_regset hook for GP registers in core files. */
88 static void
89 csky_collect_gregset (const struct regset *regset,
90 const struct regcache *regcache,
91 int regnum, void *gregs_buf, size_t len)
93 int regno, gregset_num;
94 gdb_byte *gregs = (gdb_byte *) gregs_buf ;
96 gdb_assert (len >= SIZEOF_CSKY_GREGSET);
97 gregset_num = ARRAY_SIZE (csky_gregset_offset);
99 for (regno = 0; regno < gregset_num; regno++)
101 if ((regnum == csky_gregset_offset[regno] || regnum == -1)
102 && csky_gregset_offset[regno] != -1)
103 regcache->raw_collect (regno,
104 gregs + 4 + csky_gregset_offset[regno]);
108 /* Implement the supply_regset hook for FP registers in core files. */
110 static void
111 csky_supply_fregset (const struct regset *regset,
112 struct regcache *regcache, int regnum,
113 const void *regs, size_t len)
115 int i;
116 int offset = 0;
117 struct gdbarch *gdbarch = regcache->arch ();
118 const gdb_byte *fregs = (const gdb_byte *) regs;
119 int fregset_num = ARRAY_SIZE (csky_fregset_offset);
121 gdb_assert (len >= SIZEOF_CSKY_FREGSET);
122 if (len == SIZEOF_CSKY_FREGSET)
124 for (i = 0; i < fregset_num; i++)
126 if ((regnum == csky_fregset_offset[i] || regnum == -1)
127 && csky_fregset_offset[i] != -1)
129 int num = csky_fregset_offset[i];
130 offset += register_size (gdbarch, num);
131 regcache->raw_supply (csky_fregset_offset[i], fregs + offset);
135 else if (len == SIZEOF_CSKY_FREGSET_K4X)
137 /* When kernel version >= 4.x, .reg2 size will be 400.
138 Contents is {
139 unsigned long vr[96];
140 unsigned long fcr;
141 unsigned long fesr;
142 unsigned long fid;
143 unsigned long reserved;
145 VR[96] means: (vr0~vr15) + (fr16~fr31), each Vector register is
146 128-bits, each Float register is 64 bits, the total size is
147 (4*96).
149 In addition, for fr0~fr15, each FRx is the lower 64 bits of the
150 corresponding VRx. So fr0~fr15 and vr0~vr15 regisetrs use the same
151 offset. */
152 int fcr_regno[] = {122, 123, 121}; /* fcr, fesr, fid. */
154 /* Supply vr0~vr15. */
155 for (i = 0; i < 16; i ++)
157 if (*gdbarch_register_name (gdbarch, (CSKY_VR0_REGNUM + i)) != '\0')
159 offset = 16 * i;
160 regcache->raw_supply (CSKY_VR0_REGNUM + i, fregs + offset);
163 /* Supply fr0~fr15. */
164 for (i = 0; i < 16; i ++)
166 if (*gdbarch_register_name (gdbarch, (CSKY_FR0_REGNUM + i)) != '\0')
168 offset = 16 * i;
169 regcache->raw_supply (CSKY_FR0_REGNUM + i, fregs + offset);
172 /* Supply fr16~fr31. */
173 for (i = 0; i < 16; i ++)
175 if (*gdbarch_register_name (gdbarch, (CSKY_FR16_REGNUM + i)) != '\0')
177 offset = (16 * 16) + (8 * i);
178 regcache->raw_supply (CSKY_FR16_REGNUM + i, fregs + offset);
181 /* Supply fcr, fesr, fid. */
182 for (i = 0; i < 3; i ++)
184 if (*gdbarch_register_name (gdbarch, fcr_regno[i]) != '\0')
186 offset = (16 * 16) + (16 * 8) + (4 * i);
187 regcache->raw_supply (fcr_regno[i], fregs + offset);
191 else
193 warning (_("Unknow size %s of section .reg2, can not get value"
194 " of float registers."), pulongest (len));
198 /* Implement the collect_regset hook for FP registers in core files. */
200 static void
201 csky_collect_fregset (const struct regset *regset,
202 const struct regcache *regcache,
203 int regnum, void *fregs_buf, size_t len)
205 int regno;
206 struct gdbarch *gdbarch = regcache->arch ();
207 gdb_byte *fregs = (gdb_byte *) fregs_buf ;
208 int fregset_num = ARRAY_SIZE (csky_fregset_offset);
209 int offset = 0;
211 gdb_assert (len >= SIZEOF_CSKY_FREGSET);
212 if (len == SIZEOF_CSKY_FREGSET)
214 for (regno = 0; regno < fregset_num; regno++)
216 if ((regnum == csky_fregset_offset[regno] || regnum == -1)
217 && csky_fregset_offset[regno] != -1)
219 offset += register_size (gdbarch, csky_fregset_offset[regno]);
220 regcache->raw_collect (regno, fregs + offset);
224 else if (len == SIZEOF_CSKY_FREGSET_K4X)
226 /* When kernel version >= 4.x, .reg2 size will be 400.
227 Contents is {
228 unsigned long vr[96];
229 unsigned long fcr;
230 unsigned long fesr;
231 unsigned long fid;
232 unsigned long reserved;
234 VR[96] means: (vr0~vr15) + (fr16~fr31), each Vector register is$
235 128-bits, each Float register is 64 bits, the total size is$
236 (4*96).$
238 In addition, for fr0~fr15, each FRx is the lower 64 bits of the$
239 corresponding VRx. So fr0~fr15 and vr0~vr15 regisetrs use the same$
240 offset. */
241 int i = 0;
242 int fcr_regno[] = {122, 123, 121}; /* fcr, fesr, fid. */
244 /* Supply vr0~vr15. */
245 for (i = 0; i < 16; i ++)
247 if (*gdbarch_register_name (gdbarch, (CSKY_VR0_REGNUM + i)) != '\0')
249 offset = 16 * i;
250 regcache ->raw_collect (CSKY_VR0_REGNUM + i, fregs + offset);
253 /* Supply fr16~fr31. */
254 for (i = 0; i < 16; i ++)
256 if (*gdbarch_register_name (gdbarch, (CSKY_FR16_REGNUM + i)) != '\0')
258 offset = (16 * 16) + (8 * i);
259 regcache ->raw_collect (CSKY_FR16_REGNUM + i, fregs + offset);
262 /* Supply fcr, fesr, fid. */
263 for (i = 0; i < 3; i ++)
265 if (*gdbarch_register_name (gdbarch, fcr_regno[i]) != '\0')
267 offset = (16 * 16) + (16 * 8) + (4 * i);
268 regcache ->raw_collect (fcr_regno[i], fregs + offset);
272 else
274 warning (_("Unknow size %s of section .reg2, will not set value"
275 " of float registers."), pulongest (len));
279 static const struct regset csky_regset_general =
281 NULL,
282 csky_supply_gregset,
283 csky_collect_gregset
286 static const struct regset csky_regset_float =
288 NULL,
289 csky_supply_fregset,
290 csky_collect_fregset,
291 /* Allow .reg2 to have a different size, and the size of .reg2 should
292 always be bigger than SIZEOF_CSKY_FREGSET. */
296 /* Iterate over core file register note sections. */
298 static void
299 csky_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
300 iterate_over_regset_sections_cb *cb,
301 void *cb_data,
302 const struct regcache *regcache)
304 cb (".reg", sizeof (csky_gregset_offset), sizeof (csky_gregset_offset),
305 &csky_regset_general, NULL, cb_data);
306 cb (".reg2", sizeof (csky_fregset_offset), sizeof (csky_fregset_offset),
307 &csky_regset_float, NULL, cb_data);
310 static void
311 csky_linux_rt_sigreturn_init (const struct tramp_frame *self,
312 const frame_info_ptr &this_frame,
313 struct trad_frame_cache *this_cache,
314 CORE_ADDR func)
316 int i;
317 CORE_ADDR sp = get_frame_register_unsigned (this_frame, 14);
319 CORE_ADDR base = sp + CSKY_SIGINFO_OFFSET + CSKY_SIGINFO_SIZE
320 + CSKY_UCONTEXT_SIGCONTEXT
321 + CSKY_SIGCONTEXT_SC_USP
322 + CSKY_SIGCONTEXT_SC_A0;
324 /* Set addrs of R0 ~ R13. */
325 for (i = 0; i < 14; i++)
326 trad_frame_set_reg_addr (this_cache, i, base + i * 4);
328 /* Set addrs of SP(R14) and R15. */
329 trad_frame_set_reg_addr (this_cache, 14, base - 4);
330 trad_frame_set_reg_addr (this_cache, 15, base + 4 * 14);
332 /* Set addrs of R16 ~ R31. */
333 for (i = 15; i < 31; i++)
334 trad_frame_set_reg_addr (this_cache, i, base + i * 4);
336 /* Set addrs of PSR and PC. */
337 trad_frame_set_reg_addr (this_cache, 89, base + 4 * 33);
338 trad_frame_set_reg_addr (this_cache, 72, base + 4 * 34);
340 trad_frame_set_id (this_cache, frame_id_build (sp, func));
343 static struct tramp_frame
344 csky_linux_rt_sigreturn_tramp_frame = {
345 SIGTRAMP_FRAME,
348 { CSKY_MOVI_R7_173, ULONGEST_MAX },
349 { CSKY_TRAP_0, ULONGEST_MAX },
350 { TRAMP_SENTINEL_INSN }
352 csky_linux_rt_sigreturn_init
355 static void
356 csky_linux_rt_sigreturn_init_pt_regs (const struct tramp_frame *self,
357 const frame_info_ptr &this_frame,
358 struct trad_frame_cache *this_cache,
359 CORE_ADDR func)
361 int i;
362 CORE_ADDR sp = get_frame_register_unsigned (this_frame, CSKY_SP_REGNUM);
364 CORE_ADDR base = sp + CSKY_SIGINFO_OFFSET + CSKY_SIGINFO_SIZE
365 + CSKY_UCONTEXT_SIGCONTEXT
366 + CSKY_SIGCONTEXT_PT_REGS_TLS;
368 /* LR */
369 trad_frame_set_reg_addr (this_cache, CSKY_R15_REGNUM, base);
371 /* PC */
372 trad_frame_set_reg_addr (this_cache, CSKY_PC_REGNUM, base + 4);
374 /* PSR */
375 trad_frame_set_reg_addr (this_cache, CSKY_CR0_REGNUM, base + 8);
377 /* SP */
378 trad_frame_set_reg_addr (this_cache, CSKY_SP_REGNUM, base + 12);
380 /* Set addrs of R0 ~ R13. */
381 for (i = 0; i < 14; i++)
382 trad_frame_set_reg_addr (this_cache, i, base + i * 4 + 20);
384 trad_frame_set_id (this_cache, frame_id_build (sp, func));
388 static struct tramp_frame
389 csky_linux_rt_sigreturn_tramp_frame_kernel_4x = {
390 SIGTRAMP_FRAME,
393 { CSKY_MOVI_R7_139, ULONGEST_MAX },
394 { CSKY_TRAP_0, ULONGEST_MAX },
395 { TRAMP_SENTINEL_INSN }
397 csky_linux_rt_sigreturn_init_pt_regs
400 /* Hook function for gdbarch_register_osabi. */
402 static void
403 csky_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
405 linux_init_abi (info, gdbarch, 0);
407 /* Shared library handling. */
408 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
409 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
410 set_solib_svr4_fetch_link_map_offsets (gdbarch,
411 linux_ilp32_fetch_link_map_offsets);
413 /* Enable TLS support. */
414 set_gdbarch_fetch_tls_load_module_address (gdbarch,
415 svr4_fetch_objfile_link_map);
417 /* Core file support. */
418 set_gdbarch_iterate_over_regset_sections (
419 gdbarch, csky_linux_iterate_over_regset_sections);
421 /* Append tramp frame unwinder for SIGNAL. */
423 tramp_frame_prepend_unwinder (gdbarch,
424 &csky_linux_rt_sigreturn_tramp_frame);
425 tramp_frame_prepend_unwinder (gdbarch,
426 &csky_linux_rt_sigreturn_tramp_frame_kernel_4x);
429 void _initialize_csky_linux_tdep ();
430 void
431 _initialize_csky_linux_tdep ()
433 gdbarch_register_osabi (bfd_arch_csky, 0, GDB_OSABI_LINUX,
434 csky_linux_init_abi);