Automatic date update in version.in
[binutils-gdb.git] / gdb / loongarch-linux-tdep.c
blob883245bec7e22afa9d2970d0545d254ef4bc14c2
1 /* Target-dependent code for GNU/Linux on LoongArch processors.
3 Copyright (C) 2022 Free Software Foundation, Inc.
4 Contributed by Loongson Ltd.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "glibc-tdep.h"
23 #include "inferior.h"
24 #include "linux-tdep.h"
25 #include "loongarch-tdep.h"
26 #include "solib-svr4.h"
27 #include "target-descriptions.h"
28 #include "trad-frame.h"
29 #include "tramp-frame.h"
31 /* Unpack an elf_gregset_t into GDB's register cache. */
33 static void
34 loongarch_supply_gregset (const struct regset *regset,
35 struct regcache *regcache, int regnum,
36 const void *gprs, size_t len)
38 int regsize = register_size (regcache->arch (), 0);
39 const gdb_byte *buf = nullptr;
41 if (regnum == -1)
43 regcache->raw_supply_zeroed (0);
45 for (int i = 1; i < 32; i++)
47 buf = (const gdb_byte*) gprs + regsize * i;
48 regcache->raw_supply (i, (const void *) buf);
51 buf = (const gdb_byte*) gprs + regsize * LOONGARCH_ORIG_A0_REGNUM;
52 regcache->raw_supply (LOONGARCH_ORIG_A0_REGNUM, (const void *) buf);
54 buf = (const gdb_byte*) gprs + regsize * LOONGARCH_PC_REGNUM;
55 regcache->raw_supply (LOONGARCH_PC_REGNUM, (const void *) buf);
57 buf = (const gdb_byte*) gprs + regsize * LOONGARCH_BADV_REGNUM;
58 regcache->raw_supply (LOONGARCH_BADV_REGNUM, (const void *) buf);
60 else if (regnum == 0)
61 regcache->raw_supply_zeroed (0);
62 else if ((regnum > 0 && regnum < 32)
63 || regnum == LOONGARCH_ORIG_A0_REGNUM
64 || regnum == LOONGARCH_PC_REGNUM
65 || regnum == LOONGARCH_BADV_REGNUM)
67 buf = (const gdb_byte*) gprs + regsize * regnum;
68 regcache->raw_supply (regnum, (const void *) buf);
72 /* Pack the GDB's register cache value into an elf_gregset_t. */
74 static void
75 loongarch_fill_gregset (const struct regset *regset,
76 const struct regcache *regcache, int regnum,
77 void *gprs, size_t len)
79 int regsize = register_size (regcache->arch (), 0);
80 gdb_byte *buf = nullptr;
82 if (regnum == -1)
84 for (int i = 0; i < 32; i++)
86 buf = (gdb_byte *) gprs + regsize * i;
87 regcache->raw_collect (i, (void *) buf);
90 buf = (gdb_byte *) gprs + regsize * LOONGARCH_ORIG_A0_REGNUM;
91 regcache->raw_collect (LOONGARCH_ORIG_A0_REGNUM, (void *) buf);
93 buf = (gdb_byte *) gprs + regsize * LOONGARCH_PC_REGNUM;
94 regcache->raw_collect (LOONGARCH_PC_REGNUM, (void *) buf);
96 buf = (gdb_byte *) gprs + regsize * LOONGARCH_BADV_REGNUM;
97 regcache->raw_collect (LOONGARCH_BADV_REGNUM, (void *) buf);
99 else if ((regnum >= 0 && regnum < 32)
100 || regnum == LOONGARCH_ORIG_A0_REGNUM
101 || regnum == LOONGARCH_PC_REGNUM
102 || regnum == LOONGARCH_BADV_REGNUM)
104 buf = (gdb_byte *) gprs + regsize * regnum;
105 regcache->raw_collect (regnum, (void *) buf);
109 /* Define the general register regset. */
111 const struct regset loongarch_gregset =
113 nullptr,
114 loongarch_supply_gregset,
115 loongarch_fill_gregset,
118 /* Unpack an elf_fpregset_t into GDB's register cache. */
119 static void
120 loongarch_supply_fpregset (const struct regset *r,
121 struct regcache *regcache, int regnum,
122 const void *fprs, size_t len)
124 const gdb_byte *buf = nullptr;
125 int fprsize = register_size (regcache->arch (), LOONGARCH_FIRST_FP_REGNUM);
126 int fccsize = register_size (regcache->arch (), LOONGARCH_FIRST_FCC_REGNUM);
128 if (regnum == -1)
130 for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
132 buf = (const gdb_byte *)fprs + fprsize * i;
133 regcache->raw_supply (LOONGARCH_FIRST_FP_REGNUM + i, (const void *)buf);
135 for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
137 buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
138 fccsize * i;
139 regcache->raw_supply (LOONGARCH_FIRST_FCC_REGNUM + i, (const void *)buf);
141 buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
142 fccsize * LOONGARCH_LINUX_NUM_FCC;
143 regcache->raw_supply (LOONGARCH_FCSR_REGNUM, (const void *)buf);
145 else if (regnum >= LOONGARCH_FIRST_FP_REGNUM && regnum < LOONGARCH_FIRST_FCC_REGNUM)
147 buf = (const gdb_byte *)fprs + fprsize * (regnum - LOONGARCH_FIRST_FP_REGNUM);
148 regcache->raw_supply (regnum, (const void *)buf);
150 else if (regnum >= LOONGARCH_FIRST_FCC_REGNUM && regnum < LOONGARCH_FCSR_REGNUM)
152 buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
153 fccsize * (regnum - LOONGARCH_FIRST_FCC_REGNUM);
154 regcache->raw_supply (regnum, (const void *)buf);
156 else if (regnum == LOONGARCH_FCSR_REGNUM)
158 buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
159 fccsize * LOONGARCH_LINUX_NUM_FCC;
160 regcache->raw_supply (regnum, (const void *)buf);
164 /* Pack the GDB's register cache value into an elf_fpregset_t. */
165 static void
166 loongarch_fill_fpregset (const struct regset *r,
167 const struct regcache *regcache, int regnum,
168 void *fprs, size_t len)
170 gdb_byte *buf = nullptr;
171 int fprsize = register_size (regcache->arch (), LOONGARCH_FIRST_FP_REGNUM);
172 int fccsize = register_size (regcache->arch (), LOONGARCH_FIRST_FCC_REGNUM);
174 if (regnum == -1)
176 for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
178 buf = (gdb_byte *)fprs + fprsize * i;
179 regcache->raw_collect (LOONGARCH_FIRST_FP_REGNUM + i, (void *)buf);
181 for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
183 buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
184 fccsize * i;
185 regcache->raw_collect (LOONGARCH_FIRST_FCC_REGNUM + i, (void *)buf);
187 buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
188 fccsize * LOONGARCH_LINUX_NUM_FCC;
189 regcache->raw_collect (LOONGARCH_FCSR_REGNUM, (void *)buf);
191 else if (regnum >= LOONGARCH_FIRST_FP_REGNUM && regnum < LOONGARCH_FIRST_FCC_REGNUM)
193 buf = (gdb_byte *)fprs + fprsize * (regnum - LOONGARCH_FIRST_FP_REGNUM);
194 regcache->raw_collect (regnum, (void *)buf);
196 else if (regnum >= LOONGARCH_FIRST_FCC_REGNUM && regnum < LOONGARCH_FCSR_REGNUM)
198 buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
199 fccsize * (regnum - LOONGARCH_FIRST_FCC_REGNUM);
200 regcache->raw_collect (regnum, (void *)buf);
202 else if (regnum == LOONGARCH_FCSR_REGNUM)
204 buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
205 fccsize * LOONGARCH_LINUX_NUM_FCC;
206 regcache->raw_collect (regnum, (void *)buf);
210 /* Define the FP register regset. */
211 const struct regset loongarch_fpregset =
213 nullptr,
214 loongarch_supply_fpregset,
215 loongarch_fill_fpregset,
218 /* Implement the "init" method of struct tramp_frame. */
220 #define LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET 128
221 #define LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET 176
223 static void
224 loongarch_linux_rt_sigframe_init (const struct tramp_frame *self,
225 struct frame_info *this_frame,
226 struct trad_frame_cache *this_cache,
227 CORE_ADDR func)
229 CORE_ADDR frame_sp = get_frame_sp (this_frame);
230 CORE_ADDR sigcontext_base = (frame_sp + LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET
231 + LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET);
233 trad_frame_set_reg_addr (this_cache, LOONGARCH_PC_REGNUM, sigcontext_base);
234 for (int i = 0; i < 32; i++)
235 trad_frame_set_reg_addr (this_cache, i, sigcontext_base + 8 + i * 8);
237 trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
240 /* li.w a7, __NR_rt_sigreturn */
241 #define LOONGARCH_INST_LIW_A7_RT_SIGRETURN 0x03822c0b
242 /* syscall 0 */
243 #define LOONGARCH_INST_SYSCALL 0x002b0000
245 static const struct tramp_frame loongarch_linux_rt_sigframe =
247 SIGTRAMP_FRAME,
250 { LOONGARCH_INST_LIW_A7_RT_SIGRETURN, ULONGEST_MAX },
251 { LOONGARCH_INST_SYSCALL, ULONGEST_MAX },
252 { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
254 loongarch_linux_rt_sigframe_init,
255 nullptr
258 /* Implement the "iterate_over_regset_sections" gdbarch method. */
260 static void
261 loongarch_iterate_over_regset_sections (struct gdbarch *gdbarch,
262 iterate_over_regset_sections_cb *cb,
263 void *cb_data,
264 const struct regcache *regcache)
266 int gprsize = register_size (gdbarch, 0);
267 int fprsize = register_size (gdbarch, LOONGARCH_FIRST_FP_REGNUM);
268 int fccsize = register_size (gdbarch, LOONGARCH_FIRST_FCC_REGNUM);
269 int fcsrsize = register_size (gdbarch, LOONGARCH_FCSR_REGNUM);
270 int fpsize = fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
271 fccsize * LOONGARCH_LINUX_NUM_FCC + fcsrsize;
273 cb (".reg", LOONGARCH_LINUX_NUM_GREGSET * gprsize,
274 LOONGARCH_LINUX_NUM_GREGSET * gprsize, &loongarch_gregset, nullptr, cb_data);
275 cb (".reg2", fpsize, fpsize, &loongarch_fpregset, nullptr, cb_data);
278 /* The following value is derived from __NR_rt_sigreturn in
279 <include/uapi/asm-generic/unistd.h> from the Linux source tree. */
281 #define LOONGARCH_NR_rt_sigreturn 139
283 /* When FRAME is at a syscall instruction, return the PC of the next
284 instruction to be executed. */
286 static CORE_ADDR
287 loongarch_linux_syscall_next_pc (struct frame_info *frame)
289 const CORE_ADDR pc = get_frame_pc (frame);
290 ULONGEST a7 = get_frame_register_unsigned (frame, LOONGARCH_A7_REGNUM);
292 /* If we are about to make a sigreturn syscall, use the unwinder to
293 decode the signal frame. */
294 if (a7 == LOONGARCH_NR_rt_sigreturn)
295 return frame_unwind_caller_pc (frame);
297 return pc + 4;
300 /* Initialize LoongArch Linux ABI info. */
302 static void
303 loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
305 loongarch_gdbarch_tdep *tdep = gdbarch_tdep<loongarch_gdbarch_tdep> (gdbarch);
307 linux_init_abi (info, gdbarch, 0);
309 set_solib_svr4_fetch_link_map_offsets (gdbarch,
310 info.bfd_arch_info->bits_per_address == 32
311 ? linux_ilp32_fetch_link_map_offsets
312 : linux_lp64_fetch_link_map_offsets);
314 /* GNU/Linux uses SVR4-style shared libraries. */
315 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
317 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
318 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
320 /* Enable TLS support. */
321 set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
323 /* Prepend tramp frame unwinder for signal. */
324 tramp_frame_prepend_unwinder (gdbarch, &loongarch_linux_rt_sigframe);
326 /* Core file support. */
327 set_gdbarch_iterate_over_regset_sections (gdbarch, loongarch_iterate_over_regset_sections);
329 tdep->syscall_next_pc = loongarch_linux_syscall_next_pc;
332 /* Initialize LoongArch Linux target support. */
334 void _initialize_loongarch_linux_tdep ();
335 void
336 _initialize_loongarch_linux_tdep ()
338 gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch32,
339 GDB_OSABI_LINUX, loongarch_linux_init_abi);
340 gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch64,
341 GDB_OSABI_LINUX, loongarch_linux_init_abi);