2022-02-11 20:15:06 +08:00
|
|
|
/* Target-dependent code for GNU/Linux on LoongArch processors.
|
|
|
|
|
|
|
|
Copyright (C) 2022 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"
|
2022-03-31 19:56:51 +08:00
|
|
|
#include "trad-frame.h"
|
|
|
|
#include "tramp-frame.h"
|
2022-02-11 20:15:06 +08:00
|
|
|
|
|
|
|
/* Unpack an elf_gregset_t into GDB's register cache. */
|
|
|
|
|
|
|
|
static void
|
2022-06-25 09:20:00 +08:00
|
|
|
loongarch_supply_gregset (const struct regset *regset,
|
|
|
|
struct regcache *regcache, int regnum,
|
2022-02-11 20:15:06 +08:00
|
|
|
const void *gprs, size_t len)
|
|
|
|
{
|
2022-06-25 09:20:00 +08:00
|
|
|
int regsize = register_size (regcache->arch (), 0);
|
2022-02-11 20:15:06 +08:00
|
|
|
const gdb_byte *buf = nullptr;
|
|
|
|
|
2022-06-25 09:20:00 +08:00
|
|
|
if (regnum == -1)
|
2022-02-11 20:15:06 +08:00
|
|
|
{
|
2022-06-25 09:20:00 +08:00
|
|
|
regcache->raw_supply_zeroed (0);
|
2022-02-11 20:15:06 +08:00
|
|
|
|
|
|
|
for (int i = 1; i < 32; i++)
|
|
|
|
{
|
|
|
|
buf = (const gdb_byte*) gprs + regsize * i;
|
2022-06-25 09:20:00 +08:00
|
|
|
regcache->raw_supply (i, (const void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 19:30:12 +08:00
|
|
|
buf = (const gdb_byte*) gprs + regsize * LOONGARCH_ORIG_A0_REGNUM;
|
|
|
|
regcache->raw_supply (LOONGARCH_ORIG_A0_REGNUM, (const void *) buf);
|
|
|
|
|
2022-06-25 09:20:00 +08:00
|
|
|
buf = (const gdb_byte*) gprs + regsize * LOONGARCH_PC_REGNUM;
|
|
|
|
regcache->raw_supply (LOONGARCH_PC_REGNUM, (const void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
|
2022-06-25 09:20:00 +08:00
|
|
|
buf = (const gdb_byte*) gprs + regsize * LOONGARCH_BADV_REGNUM;
|
|
|
|
regcache->raw_supply (LOONGARCH_BADV_REGNUM, (const void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|
2022-06-25 09:20:00 +08:00
|
|
|
else if (regnum == 0)
|
|
|
|
regcache->raw_supply_zeroed (0);
|
|
|
|
else if ((regnum > 0 && regnum < 32)
|
2022-07-05 19:30:12 +08:00
|
|
|
|| regnum == LOONGARCH_ORIG_A0_REGNUM
|
2022-06-25 09:20:00 +08:00
|
|
|
|| regnum == LOONGARCH_PC_REGNUM
|
|
|
|
|| regnum == LOONGARCH_BADV_REGNUM)
|
2022-02-11 20:15:06 +08:00
|
|
|
{
|
2022-06-25 09:20:00 +08:00
|
|
|
buf = (const gdb_byte*) gprs + regsize * regnum;
|
|
|
|
regcache->raw_supply (regnum, (const void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pack the GDB's register cache value into an elf_gregset_t. */
|
|
|
|
|
|
|
|
static void
|
2022-06-25 09:20:00 +08:00
|
|
|
loongarch_fill_gregset (const struct regset *regset,
|
|
|
|
const struct regcache *regcache, int regnum,
|
2022-02-11 20:15:06 +08:00
|
|
|
void *gprs, size_t len)
|
|
|
|
{
|
2022-06-25 09:20:00 +08:00
|
|
|
int regsize = register_size (regcache->arch (), 0);
|
2022-02-11 20:15:06 +08:00
|
|
|
gdb_byte *buf = nullptr;
|
|
|
|
|
2022-06-25 09:20:00 +08:00
|
|
|
if (regnum == -1)
|
2022-02-11 20:15:06 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
buf = (gdb_byte *) gprs + regsize * i;
|
2022-06-25 09:20:00 +08:00
|
|
|
regcache->raw_collect (i, (void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|
|
|
|
|
2022-07-05 19:30:12 +08:00
|
|
|
buf = (gdb_byte *) gprs + regsize * LOONGARCH_ORIG_A0_REGNUM;
|
|
|
|
regcache->raw_collect (LOONGARCH_ORIG_A0_REGNUM, (void *) buf);
|
|
|
|
|
2022-06-25 09:20:00 +08:00
|
|
|
buf = (gdb_byte *) gprs + regsize * LOONGARCH_PC_REGNUM;
|
|
|
|
regcache->raw_collect (LOONGARCH_PC_REGNUM, (void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
|
2022-06-25 09:20:00 +08:00
|
|
|
buf = (gdb_byte *) gprs + regsize * LOONGARCH_BADV_REGNUM;
|
|
|
|
regcache->raw_collect (LOONGARCH_BADV_REGNUM, (void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|
2022-06-25 09:20:00 +08:00
|
|
|
else if ((regnum >= 0 && regnum < 32)
|
2022-07-05 19:30:12 +08:00
|
|
|
|| regnum == LOONGARCH_ORIG_A0_REGNUM
|
2022-06-25 09:20:00 +08:00
|
|
|
|| regnum == LOONGARCH_PC_REGNUM
|
|
|
|
|| regnum == LOONGARCH_BADV_REGNUM)
|
2022-02-11 20:15:06 +08:00
|
|
|
{
|
2022-06-25 09:20:00 +08:00
|
|
|
buf = (gdb_byte *) gprs + regsize * regnum;
|
|
|
|
regcache->raw_collect (regnum, (void *) buf);
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register set definitions. */
|
|
|
|
|
|
|
|
const struct regset loongarch_gregset =
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
loongarch_supply_gregset,
|
|
|
|
loongarch_fill_gregset,
|
|
|
|
};
|
|
|
|
|
2022-03-31 19:56:51 +08:00
|
|
|
/* 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,
|
|
|
|
struct frame_info *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);
|
|
|
|
|
2022-06-25 09:20:00 +08:00
|
|
|
trad_frame_set_reg_addr (this_cache, LOONGARCH_PC_REGNUM, sigcontext_base);
|
2022-03-31 19:56:51 +08:00
|
|
|
for (int i = 0; i < 32; i++)
|
2022-06-25 09:20:00 +08:00
|
|
|
trad_frame_set_reg_addr (this_cache, i, sigcontext_base + 8 + i * 8);
|
2022-03-31 19:56:51 +08:00
|
|
|
|
|
|
|
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,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-05-23 15:53:06 +08:00
|
|
|
/* 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)
|
|
|
|
{
|
2022-06-25 09:20:00 +08:00
|
|
|
int regsize = register_size (gdbarch, 0);
|
2022-05-24 01:59:13 +08:00
|
|
|
|
|
|
|
cb (".reg", LOONGARCH_LINUX_NUM_GREGSET * regsize,
|
|
|
|
LOONGARCH_LINUX_NUM_GREGSET * regsize, &loongarch_gregset, NULL, cb_data);
|
2022-05-23 15:53:06 +08:00
|
|
|
}
|
|
|
|
|
2022-06-25 09:21:16 +08:00
|
|
|
/* 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 (struct frame_info *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;
|
|
|
|
}
|
|
|
|
|
2022-02-11 20:15:06 +08:00
|
|
|
/* Initialize LoongArch Linux ABI info. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
|
|
|
|
{
|
2022-06-25 09:21:16 +08:00
|
|
|
loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
|
|
|
|
|
2022-02-11 20:15:06 +08:00
|
|
|
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);
|
2022-03-31 19:56:51 +08:00
|
|
|
|
2022-02-11 20:15:06 +08:00
|
|
|
/* Enable TLS support. */
|
|
|
|
set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
|
2022-03-31 19:56:51 +08:00
|
|
|
|
|
|
|
/* Prepend tramp frame unwinder for signal. */
|
|
|
|
tramp_frame_prepend_unwinder (gdbarch, &loongarch_linux_rt_sigframe);
|
2022-05-23 15:53:06 +08:00
|
|
|
|
|
|
|
/* Core file support. */
|
|
|
|
set_gdbarch_iterate_over_regset_sections (gdbarch, loongarch_iterate_over_regset_sections);
|
2022-06-25 09:21:16 +08:00
|
|
|
|
|
|
|
tdep->syscall_next_pc = loongarch_linux_syscall_next_pc;
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|