1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/* Target-dependent code for GNU/Linux on LoongArch processors.
Copyright (C) 2022-2024 Free Software Foundation, Inc.
Contributed by Loongson Ltd.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "glibc-tdep.h"
#include "inferior.h"
#include "linux-tdep.h"
#include "loongarch-tdep.h"
#include "solib-svr4.h"
#include "target-descriptions.h"
#include "trad-frame.h"
#include "tramp-frame.h"
/* Unpack an elf_gregset_t into GDB's register cache. */
static void
loongarch_supply_gregset (const struct regset *regset,
struct regcache *regcache, int regnum,
const void *gprs, size_t len)
{
int regsize = register_size (regcache->arch (), 0);
const gdb_byte *buf = nullptr;
if (regnum == -1)
{
regcache->raw_supply_zeroed (0);
for (int i = 1; i < 32; i++)
{
buf = (const gdb_byte*) gprs + regsize * i;
regcache->raw_supply (i, (const void *) buf);
}
buf = (const gdb_byte*) gprs + regsize * LOONGARCH_ORIG_A0_REGNUM;
regcache->raw_supply (LOONGARCH_ORIG_A0_REGNUM, (const void *) buf);
buf = (const gdb_byte*) gprs + regsize * LOONGARCH_PC_REGNUM;
regcache->raw_supply (LOONGARCH_PC_REGNUM, (const void *) buf);
buf = (const gdb_byte*) gprs + regsize * LOONGARCH_BADV_REGNUM;
regcache->raw_supply (LOONGARCH_BADV_REGNUM, (const void *) buf);
}
else if (regnum == 0)
regcache->raw_supply_zeroed (0);
else if ((regnum > 0 && regnum < 32)
|| regnum == LOONGARCH_ORIG_A0_REGNUM
|| regnum == LOONGARCH_PC_REGNUM
|| regnum == LOONGARCH_BADV_REGNUM)
{
buf = (const gdb_byte*) gprs + regsize * regnum;
regcache->raw_supply (regnum, (const void *) buf);
}
}
/* Pack the GDB's register cache value into an elf_gregset_t. */
static void
loongarch_fill_gregset (const struct regset *regset,
const struct regcache *regcache, int regnum,
void *gprs, size_t len)
{
int regsize = register_size (regcache->arch (), 0);
gdb_byte *buf = nullptr;
if (regnum == -1)
{
for (int i = 0; i < 32; i++)
{
buf = (gdb_byte *) gprs + regsize * i;
regcache->raw_collect (i, (void *) buf);
}
buf = (gdb_byte *) gprs + regsize * LOONGARCH_ORIG_A0_REGNUM;
regcache->raw_collect (LOONGARCH_ORIG_A0_REGNUM, (void *) buf);
buf = (gdb_byte *) gprs + regsize * LOONGARCH_PC_REGNUM;
regcache->raw_collect (LOONGARCH_PC_REGNUM, (void *) buf);
buf = (gdb_byte *) gprs + regsize * LOONGARCH_BADV_REGNUM;
regcache->raw_collect (LOONGARCH_BADV_REGNUM, (void *) buf);
}
else if ((regnum >= 0 && regnum < 32)
|| regnum == LOONGARCH_ORIG_A0_REGNUM
|| regnum == LOONGARCH_PC_REGNUM
|| regnum == LOONGARCH_BADV_REGNUM)
{
buf = (gdb_byte *) gprs + regsize * regnum;
regcache->raw_collect (regnum, (void *) buf);
}
}
/* Define the general register regset. */
const struct regset loongarch_gregset =
{
nullptr,
loongarch_supply_gregset,
loongarch_fill_gregset,
};
/* Unpack an elf_fpregset_t into GDB's register cache. */
static void
loongarch_supply_fpregset (const struct regset *r,
struct regcache *regcache, int regnum,
const void *fprs, size_t len)
{
const gdb_byte *buf = nullptr;
int fprsize = register_size (regcache->arch (), LOONGARCH_FIRST_FP_REGNUM);
int fccsize = register_size (regcache->arch (), LOONGARCH_FIRST_FCC_REGNUM);
if (regnum == -1)
{
for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
{
buf = (const gdb_byte *)fprs + fprsize * i;
regcache->raw_supply (LOONGARCH_FIRST_FP_REGNUM + i, (const void *)buf);
}
for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
{
buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * i;
regcache->raw_supply (LOONGARCH_FIRST_FCC_REGNUM + i, (const void *)buf);
}
buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * LOONGARCH_LINUX_NUM_FCC;
regcache->raw_supply (LOONGARCH_FCSR_REGNUM, (const void *)buf);
}
else if (regnum >= LOONGARCH_FIRST_FP_REGNUM && regnum < LOONGARCH_FIRST_FCC_REGNUM)
{
buf = (const gdb_byte *)fprs + fprsize * (regnum - LOONGARCH_FIRST_FP_REGNUM);
regcache->raw_supply (regnum, (const void *)buf);
}
else if (regnum >= LOONGARCH_FIRST_FCC_REGNUM && regnum < LOONGARCH_FCSR_REGNUM)
{
buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * (regnum - LOONGARCH_FIRST_FCC_REGNUM);
regcache->raw_supply (regnum, (const void *)buf);
}
else if (regnum == LOONGARCH_FCSR_REGNUM)
{
buf = (const gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * LOONGARCH_LINUX_NUM_FCC;
regcache->raw_supply (regnum, (const void *)buf);
}
}
/* Pack the GDB's register cache value into an elf_fpregset_t. */
static void
loongarch_fill_fpregset (const struct regset *r,
const struct regcache *regcache, int regnum,
void *fprs, size_t len)
{
gdb_byte *buf = nullptr;
int fprsize = register_size (regcache->arch (), LOONGARCH_FIRST_FP_REGNUM);
int fccsize = register_size (regcache->arch (), LOONGARCH_FIRST_FCC_REGNUM);
if (regnum == -1)
{
for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
{
buf = (gdb_byte *)fprs + fprsize * i;
regcache->raw_collect (LOONGARCH_FIRST_FP_REGNUM + i, (void *)buf);
}
for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
{
buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * i;
regcache->raw_collect (LOONGARCH_FIRST_FCC_REGNUM + i, (void *)buf);
}
buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * LOONGARCH_LINUX_NUM_FCC;
regcache->raw_collect (LOONGARCH_FCSR_REGNUM, (void *)buf);
}
else if (regnum >= LOONGARCH_FIRST_FP_REGNUM && regnum < LOONGARCH_FIRST_FCC_REGNUM)
{
buf = (gdb_byte *)fprs + fprsize * (regnum - LOONGARCH_FIRST_FP_REGNUM);
regcache->raw_collect (regnum, (void *)buf);
}
else if (regnum >= LOONGARCH_FIRST_FCC_REGNUM && regnum < LOONGARCH_FCSR_REGNUM)
{
buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * (regnum - LOONGARCH_FIRST_FCC_REGNUM);
regcache->raw_collect (regnum, (void *)buf);
}
else if (regnum == LOONGARCH_FCSR_REGNUM)
{
buf = (gdb_byte *)fprs + fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * LOONGARCH_LINUX_NUM_FCC;
regcache->raw_collect (regnum, (void *)buf);
}
}
/* Define the FP register regset. */
const struct regset loongarch_fpregset =
{
nullptr,
loongarch_supply_fpregset,
loongarch_fill_fpregset,
};
/* Unpack elf_lsxregset_t into GDB's register cache. */
static void
loongarch_supply_lsxregset (const struct regset *regset,
struct regcache *regcache, int regnum,
const void *lsxrs, size_t len)
{
int lsxrsize = register_size (regcache->arch (), LOONGARCH_FIRST_LSX_REGNUM);
const gdb_byte *buf = nullptr;
if (regnum == -1)
{
for (int i = 0; i < LOONGARCH_LINUX_NUM_LSXREGSET; i++)
{
buf = (const gdb_byte*) lsxrs + lsxrsize * i;
regcache->raw_supply (LOONGARCH_FIRST_LSX_REGNUM + i, (const void *) buf);
}
}
else if (regnum >= LOONGARCH_FIRST_LSX_REGNUM && regnum < LOONGARCH_FIRST_LASX_REGNUM)
{
buf = (const gdb_byte*) lsxrs + lsxrsize * (regnum - LOONGARCH_FIRST_LSX_REGNUM);
regcache->raw_supply (regnum, (const void *) buf);
}
}
/* Pack the GDB's register cache value into an elf_lsxregset_t. */
static void
loongarch_fill_lsxregset (const struct regset *regset,
const struct regcache *regcache, int regnum,
void *lsxrs, size_t len)
{
int lsxrsize = register_size (regcache->arch (), LOONGARCH_FIRST_LSX_REGNUM);
gdb_byte *buf = nullptr;
if (regnum == -1)
{
for (int i = 0; i < LOONGARCH_LINUX_NUM_LSXREGSET; i++)
{
buf = (gdb_byte *) lsxrs + lsxrsize * i;
regcache->raw_collect (LOONGARCH_FIRST_LSX_REGNUM + i, (void *) buf);
}
}
else if (regnum >= LOONGARCH_FIRST_LSX_REGNUM && regnum < LOONGARCH_FIRST_LASX_REGNUM)
{
buf = (gdb_byte *) lsxrs + lsxrsize * (regnum - LOONGARCH_FIRST_LSX_REGNUM);
regcache->raw_collect (regnum, (void *) buf);
}
}
/* Define the Loongson SIMD Extension register regset. */
const struct regset loongarch_lsxregset =
{
nullptr,
loongarch_supply_lsxregset,
loongarch_fill_lsxregset,
};
/* Unpack elf_lasxregset_t into GDB's register cache. */
static void
loongarch_supply_lasxregset (const struct regset *regset,
struct regcache *regcache, int regnum,
const void *lasxrs, size_t len)
{
int lasxrsize = register_size (regcache->arch (), LOONGARCH_FIRST_LASX_REGNUM);
const gdb_byte *buf = nullptr;
if (regnum == -1)
{
for (int i = 0; i < LOONGARCH_LINUX_NUM_LASXREGSET; i++)
{
buf = (const gdb_byte*) lasxrs + lasxrsize * i;
regcache->raw_supply (LOONGARCH_FIRST_LASX_REGNUM + i, (const void *) buf);
}
}
else if (regnum >= LOONGARCH_FIRST_LASX_REGNUM
&& regnum < LOONGARCH_FIRST_LASX_REGNUM + LOONGARCH_LINUX_NUM_LASXREGSET)
{
buf = (const gdb_byte*) lasxrs + lasxrsize * (regnum - LOONGARCH_FIRST_LASX_REGNUM);
regcache->raw_supply (regnum, (const void *) buf);
}
}
/* Pack the GDB's register cache value into an elf_lasxregset_t. */
static void
loongarch_fill_lasxregset (const struct regset *regset,
const struct regcache *regcache, int regnum,
void *lasxrs, size_t len)
{
int lasxrsize = register_size (regcache->arch (), LOONGARCH_FIRST_LASX_REGNUM);
gdb_byte *buf = nullptr;
if (regnum == -1)
{
for (int i = 0; i < LOONGARCH_LINUX_NUM_LASXREGSET; i++)
{
buf = (gdb_byte *) lasxrs + lasxrsize * i;
regcache->raw_collect (LOONGARCH_FIRST_LASX_REGNUM + i, (void *) buf);
}
}
else if (regnum >= LOONGARCH_FIRST_LASX_REGNUM
&& regnum < LOONGARCH_FIRST_LASX_REGNUM + LOONGARCH_LINUX_NUM_LASXREGSET)
{
buf = (gdb_byte *) lasxrs + lasxrsize * (regnum - LOONGARCH_FIRST_LASX_REGNUM);
regcache->raw_collect (regnum, (void *) buf);
}
}
/* Define the Loongson Advanced SIMD Extension register regset. */
const struct regset loongarch_lasxregset =
{
nullptr,
loongarch_supply_lasxregset,
loongarch_fill_lasxregset,
};
/* Implement the "init" method of struct tramp_frame. */
#define LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET 128
#define LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET 176
static void
loongarch_linux_rt_sigframe_init (const struct tramp_frame *self,
frame_info_ptr this_frame,
struct trad_frame_cache *this_cache,
CORE_ADDR func)
{
CORE_ADDR frame_sp = get_frame_sp (this_frame);
CORE_ADDR sigcontext_base = (frame_sp + LOONGARCH_RT_SIGFRAME_UCONTEXT_OFFSET
+ LOONGARCH_UCONTEXT_SIGCONTEXT_OFFSET);
trad_frame_set_reg_addr (this_cache, LOONGARCH_PC_REGNUM, sigcontext_base);
for (int i = 0; i < 32; i++)
trad_frame_set_reg_addr (this_cache, i, sigcontext_base + 8 + i * 8);
trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
}
/* li.w a7, __NR_rt_sigreturn */
#define LOONGARCH_INST_LIW_A7_RT_SIGRETURN 0x03822c0b
/* syscall 0 */
#define LOONGARCH_INST_SYSCALL 0x002b0000
static const struct tramp_frame loongarch_linux_rt_sigframe =
{
SIGTRAMP_FRAME,
4,
{
{ LOONGARCH_INST_LIW_A7_RT_SIGRETURN, ULONGEST_MAX },
{ LOONGARCH_INST_SYSCALL, ULONGEST_MAX },
{ TRAMP_SENTINEL_INSN, ULONGEST_MAX }
},
loongarch_linux_rt_sigframe_init,
nullptr
};
/* Implement the "iterate_over_regset_sections" gdbarch method. */
static void
loongarch_iterate_over_regset_sections (struct gdbarch *gdbarch,
iterate_over_regset_sections_cb *cb,
void *cb_data,
const struct regcache *regcache)
{
int gprsize = register_size (gdbarch, 0);
int fprsize = register_size (gdbarch, LOONGARCH_FIRST_FP_REGNUM);
int fccsize = register_size (gdbarch, LOONGARCH_FIRST_FCC_REGNUM);
int fcsrsize = register_size (gdbarch, LOONGARCH_FCSR_REGNUM);
int fpsize = fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
fccsize * LOONGARCH_LINUX_NUM_FCC + fcsrsize;
int lsxrsize = register_size (gdbarch, LOONGARCH_FIRST_LSX_REGNUM);
int lasxrsize = register_size (gdbarch, LOONGARCH_FIRST_LASX_REGNUM);
cb (".reg", LOONGARCH_LINUX_NUM_GREGSET * gprsize,
LOONGARCH_LINUX_NUM_GREGSET * gprsize, &loongarch_gregset, nullptr, cb_data);
cb (".reg2", fpsize, fpsize, &loongarch_fpregset, nullptr, cb_data);
cb (".reg-loongarch-lsx", lsxrsize, lsxrsize,
&loongarch_lsxregset, nullptr, cb_data);
cb (".reg-loongarch-lasx", lasxrsize, lasxrsize,
&loongarch_lasxregset, nullptr, cb_data);
}
/* The following value is derived from __NR_rt_sigreturn in
<include/uapi/asm-generic/unistd.h> from the Linux source tree. */
#define LOONGARCH_NR_rt_sigreturn 139
/* When FRAME is at a syscall instruction, return the PC of the next
instruction to be executed. */
static CORE_ADDR
loongarch_linux_syscall_next_pc (frame_info_ptr frame)
{
const CORE_ADDR pc = get_frame_pc (frame);
ULONGEST a7 = get_frame_register_unsigned (frame, LOONGARCH_A7_REGNUM);
/* If we are about to make a sigreturn syscall, use the unwinder to
decode the signal frame. */
if (a7 == LOONGARCH_NR_rt_sigreturn)
return frame_unwind_caller_pc (frame);
return pc + 4;
}
/* Initialize LoongArch Linux ABI info. */
static void
loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
loongarch_gdbarch_tdep *tdep = gdbarch_tdep<loongarch_gdbarch_tdep> (gdbarch);
linux_init_abi (info, gdbarch, 0);
set_solib_svr4_fetch_link_map_offsets (gdbarch,
info.bfd_arch_info->bits_per_address == 32
? linux_ilp32_fetch_link_map_offsets
: linux_lp64_fetch_link_map_offsets);
/* GNU/Linux uses SVR4-style shared libraries. */
set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
/* GNU/Linux uses the dynamic linker included in the GNU C Library. */
set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
/* Enable TLS support. */
set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
/* Prepend tramp frame unwinder for signal. */
tramp_frame_prepend_unwinder (gdbarch, &loongarch_linux_rt_sigframe);
/* Core file support. */
set_gdbarch_iterate_over_regset_sections (gdbarch, loongarch_iterate_over_regset_sections);
tdep->syscall_next_pc = loongarch_linux_syscall_next_pc;
}
/* Initialize LoongArch Linux target support. */
void _initialize_loongarch_linux_tdep ();
void
_initialize_loongarch_linux_tdep ()
{
gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch32,
GDB_OSABI_LINUX, loongarch_linux_init_abi);
gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch64,
GDB_OSABI_LINUX, loongarch_linux_init_abi);
}
|