2022-02-11 20:15:06 +08:00
|
|
|
/* Target-dependent code for GNU/Linux on LoongArch processors.
|
|
|
|
|
2024-01-12 23:30:44 +08:00
|
|
|
Copyright (C) 2022-2024 Free Software Foundation, Inc.
|
2022-02-11 20:15:06 +08:00
|
|
|
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/>. */
|
|
|
|
|
2024-04-23 04:10:14 +08:00
|
|
|
#include "extract-store-integer.h"
|
2022-02-11 20:15:06 +08:00
|
|
|
#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"
|
2024-02-29 16:39:10 +08:00
|
|
|
#include "xml-syscall.h"
|
|
|
|
|
|
|
|
/* The syscall's XML filename for LoongArch. */
|
|
|
|
#define XML_SYSCALL_FILENAME_LOONGARCH "syscalls/loongarch-linux.xml"
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:33:28 +08:00
|
|
|
/* Define the general register regset. */
|
2022-02-11 20:15:06 +08:00
|
|
|
|
|
|
|
const struct regset loongarch_gregset =
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
loongarch_supply_gregset,
|
|
|
|
loongarch_fill_gregset,
|
|
|
|
};
|
|
|
|
|
2022-07-12 10:33:28 +08:00
|
|
|
/* 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);
|
2022-08-02 17:16:56 +08:00
|
|
|
int fccsize = register_size (regcache->arch (), LOONGARCH_FIRST_FCC_REGNUM);
|
2022-07-12 10:33:28 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-08-02 17:16:56 +08:00
|
|
|
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);
|
2022-07-12 10:33:28 +08:00
|
|
|
}
|
2022-08-02 17:16:56 +08:00
|
|
|
else if (regnum >= LOONGARCH_FIRST_FP_REGNUM && regnum < LOONGARCH_FIRST_FCC_REGNUM)
|
2022-07-12 10:33:28 +08:00
|
|
|
{
|
|
|
|
buf = (const gdb_byte *)fprs + fprsize * (regnum - LOONGARCH_FIRST_FP_REGNUM);
|
|
|
|
regcache->raw_supply (regnum, (const void *)buf);
|
|
|
|
}
|
2022-08-02 17:16:56 +08:00
|
|
|
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);
|
|
|
|
}
|
2022-07-12 10:33:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2022-08-02 17:16:56 +08:00
|
|
|
int fccsize = register_size (regcache->arch (), LOONGARCH_FIRST_FCC_REGNUM);
|
2022-07-12 10:33:28 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-08-02 17:16:56 +08:00
|
|
|
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);
|
2022-07-12 10:33:28 +08:00
|
|
|
}
|
2022-08-02 17:16:56 +08:00
|
|
|
else if (regnum >= LOONGARCH_FIRST_FP_REGNUM && regnum < LOONGARCH_FIRST_FCC_REGNUM)
|
2022-07-12 10:33:28 +08:00
|
|
|
{
|
|
|
|
buf = (gdb_byte *)fprs + fprsize * (regnum - LOONGARCH_FIRST_FP_REGNUM);
|
|
|
|
regcache->raw_collect (regnum, (void *)buf);
|
|
|
|
}
|
2022-08-02 17:16:56 +08:00
|
|
|
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);
|
|
|
|
}
|
2022-07-12 10:33:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Define the FP register regset. */
|
|
|
|
const struct regset loongarch_fpregset =
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
loongarch_supply_fpregset,
|
|
|
|
loongarch_fill_fpregset,
|
|
|
|
};
|
|
|
|
|
2024-01-25 16:32:35 +08:00
|
|
|
/* 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,
|
|
|
|
};
|
|
|
|
|
2024-01-25 16:32:36 +08:00
|
|
|
/* Unpack an lbt regset into GDB's register cache. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
loongarch_supply_lbtregset (const struct regset *regset,
|
|
|
|
struct regcache *regcache, int regnum,
|
|
|
|
const void *regs, size_t len)
|
|
|
|
{
|
|
|
|
int scrsize = register_size (regcache->arch (), LOONGARCH_FIRST_SCR_REGNUM);
|
|
|
|
int eflagssize = register_size (regcache->arch (), LOONGARCH_EFLAGS_REGNUM);
|
|
|
|
const gdb_byte *buf = nullptr;
|
|
|
|
|
|
|
|
if (regnum == -1)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < LOONGARCH_LINUX_NUM_SCR; i++)
|
|
|
|
{
|
|
|
|
buf = (const gdb_byte *) regs + scrsize * i;
|
|
|
|
regcache->raw_supply (LOONGARCH_FIRST_SCR_REGNUM + i,
|
|
|
|
(const void *) buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = (const gdb_byte*) regs + scrsize * LOONGARCH_LINUX_NUM_SCR;
|
|
|
|
regcache->raw_supply (LOONGARCH_EFLAGS_REGNUM, (const void *) buf);
|
|
|
|
|
|
|
|
buf = (const gdb_byte*) regs
|
|
|
|
+ scrsize * LOONGARCH_LINUX_NUM_SCR
|
|
|
|
+ eflagssize;
|
|
|
|
regcache->raw_supply (LOONGARCH_FTOP_REGNUM, (const void *) buf);
|
|
|
|
}
|
|
|
|
else if (regnum >= LOONGARCH_FIRST_SCR_REGNUM
|
|
|
|
&& regnum <= LOONGARCH_LAST_SCR_REGNUM)
|
|
|
|
{
|
|
|
|
buf = (const gdb_byte*) regs
|
|
|
|
+ scrsize * (regnum - LOONGARCH_FIRST_SCR_REGNUM);
|
|
|
|
regcache->raw_supply (regnum, (const void *) buf);
|
|
|
|
}
|
|
|
|
else if (regnum == LOONGARCH_EFLAGS_REGNUM)
|
|
|
|
{
|
|
|
|
buf = (const gdb_byte*) regs + scrsize * LOONGARCH_LINUX_NUM_SCR;
|
|
|
|
regcache->raw_supply (regnum, (const void *) buf);
|
|
|
|
}
|
|
|
|
else if (regnum == LOONGARCH_FTOP_REGNUM)
|
|
|
|
{
|
|
|
|
buf = (const gdb_byte*) regs
|
|
|
|
+ scrsize * LOONGARCH_LINUX_NUM_SCR
|
|
|
|
+ eflagssize;
|
|
|
|
regcache->raw_supply (regnum, (const void *) buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pack the GDB's register cache value into an lbt regset. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
loongarch_fill_lbtregset (const struct regset *regset,
|
|
|
|
const struct regcache *regcache, int regnum,
|
|
|
|
void *regs, size_t len)
|
|
|
|
{
|
|
|
|
int scrsize = register_size (regcache->arch (), LOONGARCH_FIRST_SCR_REGNUM);
|
|
|
|
int eflagssize = register_size (regcache->arch (), LOONGARCH_EFLAGS_REGNUM);
|
|
|
|
gdb_byte *buf = nullptr;
|
|
|
|
|
|
|
|
if (regnum == -1)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < LOONGARCH_LINUX_NUM_SCR; i++)
|
|
|
|
{
|
|
|
|
buf = (gdb_byte *) regs + scrsize * i;
|
|
|
|
regcache->raw_collect (LOONGARCH_FIRST_SCR_REGNUM + i, (void *) buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = (gdb_byte *) regs + scrsize * LOONGARCH_LINUX_NUM_SCR;
|
|
|
|
regcache->raw_collect (LOONGARCH_EFLAGS_REGNUM, (void *) buf);
|
|
|
|
|
|
|
|
buf = (gdb_byte *) regs + scrsize * LOONGARCH_LINUX_NUM_SCR + eflagssize;
|
|
|
|
regcache->raw_collect (LOONGARCH_FTOP_REGNUM, (void *) buf);
|
|
|
|
}
|
|
|
|
else if (regnum >= LOONGARCH_FIRST_SCR_REGNUM
|
|
|
|
&& regnum <= LOONGARCH_LAST_SCR_REGNUM)
|
|
|
|
{
|
|
|
|
buf = (gdb_byte *) regs + scrsize * (regnum - LOONGARCH_FIRST_SCR_REGNUM);
|
|
|
|
regcache->raw_collect (regnum, (void *) buf);
|
|
|
|
}
|
|
|
|
else if (regnum == LOONGARCH_EFLAGS_REGNUM)
|
|
|
|
{
|
|
|
|
buf = (gdb_byte *) regs + scrsize * LOONGARCH_LINUX_NUM_SCR;
|
|
|
|
regcache->raw_collect (regnum, (void *) buf);
|
|
|
|
}
|
|
|
|
else if (regnum == LOONGARCH_FTOP_REGNUM)
|
|
|
|
{
|
|
|
|
buf = (gdb_byte *) regs + scrsize * LOONGARCH_LINUX_NUM_SCR + eflagssize;
|
|
|
|
regcache->raw_collect (regnum, (void *) buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Define the lbt register regset. */
|
|
|
|
|
|
|
|
const struct regset loongarch_lbtregset =
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
loongarch_supply_lbtregset,
|
|
|
|
loongarch_fill_lbtregset,
|
|
|
|
};
|
|
|
|
|
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,
|
gdb: pass frames as `const frame_info_ptr &`
We currently pass frames to function by value, as `frame_info_ptr`.
This is somewhat expensive:
- the size of `frame_info_ptr` is 64 bytes, which is a bit big to pass
by value
- the constructors and destructor link/unlink the object in the global
`frame_info_ptr::frame_list` list. This is an `intrusive_list`, so
it's not so bad: it's just assigning a few points, there's no memory
allocation as if it was `std::list`, but still it's useless to do
that over and over.
As suggested by Tom Tromey, change many function signatures to accept
`const frame_info_ptr &` instead of `frame_info_ptr`.
Some functions reassign their `frame_info_ptr` parameter, like:
void
the_func (frame_info_ptr frame)
{
for (; frame != nullptr; frame = get_prev_frame (frame))
{
...
}
}
I wondered what to do about them, do I leave them as-is or change them
(and need to introduce a separate local variable that can be
re-assigned). I opted for the later for consistency. It might not be
clear why some functions take `const frame_info_ptr &` while others take
`frame_info_ptr`. Also, if a function took a `frame_info_ptr` because
it did re-assign its parameter, I doubt that we would think to change it
to `const frame_info_ptr &` should the implementation change such that
it doesn't need to take `frame_info_ptr` anymore. It seems better to
have a simple rule and apply it everywhere.
Change-Id: I59d10addef687d157f82ccf4d54f5dde9a963fd0
Approved-By: Andrew Burgess <aburgess@redhat.com>
2024-02-20 02:07:47 +08:00
|
|
|
const frame_info_ptr &this_frame,
|
2022-03-31 19:56:51 +08:00
|
|
|
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,
|
2022-07-12 10:33:28 +08:00
|
|
|
nullptr
|
2022-03-31 19:56:51 +08:00
|
|
|
};
|
|
|
|
|
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-07-12 10:33:28 +08:00
|
|
|
int gprsize = register_size (gdbarch, 0);
|
2024-03-22 11:35:02 +08:00
|
|
|
int gpsize = gprsize * LOONGARCH_LINUX_NUM_GREGSET;
|
2022-07-12 10:33:28 +08:00
|
|
|
int fprsize = register_size (gdbarch, LOONGARCH_FIRST_FP_REGNUM);
|
2022-08-02 17:16:56 +08:00
|
|
|
int fccsize = register_size (gdbarch, LOONGARCH_FIRST_FCC_REGNUM);
|
|
|
|
int fcsrsize = register_size (gdbarch, LOONGARCH_FCSR_REGNUM);
|
|
|
|
int fpsize = fprsize * LOONGARCH_LINUX_NUM_FPREGSET +
|
2024-03-22 11:35:02 +08:00
|
|
|
fccsize * LOONGARCH_LINUX_NUM_FCC + fcsrsize;
|
2024-01-25 16:32:35 +08:00
|
|
|
int lsxrsize = register_size (gdbarch, LOONGARCH_FIRST_LSX_REGNUM);
|
2024-03-21 09:41:30 +08:00
|
|
|
int lsxsize = lsxrsize * LOONGARCH_LINUX_NUM_LSXREGSET;
|
2024-01-25 16:32:35 +08:00
|
|
|
int lasxrsize = register_size (gdbarch, LOONGARCH_FIRST_LASX_REGNUM);
|
2024-03-21 09:41:30 +08:00
|
|
|
int lasxsize = lasxrsize * LOONGARCH_LINUX_NUM_LASXREGSET;
|
2024-01-25 16:32:36 +08:00
|
|
|
int scrsize = register_size (gdbarch, LOONGARCH_FIRST_SCR_REGNUM);
|
|
|
|
int eflagssize = register_size (gdbarch, LOONGARCH_EFLAGS_REGNUM);
|
|
|
|
int ftopsize = register_size (gdbarch, LOONGARCH_FTOP_REGNUM);
|
|
|
|
int lbtsize = scrsize * LOONGARCH_LINUX_NUM_SCR + eflagssize + ftopsize;
|
2022-05-24 01:59:13 +08:00
|
|
|
|
2024-03-22 11:35:02 +08:00
|
|
|
cb (".reg", gpsize, gpsize,
|
|
|
|
&loongarch_gregset, nullptr, cb_data);
|
|
|
|
cb (".reg2", fpsize, fpsize,
|
|
|
|
&loongarch_fpregset, nullptr, cb_data);
|
2024-03-21 09:41:30 +08:00
|
|
|
cb (".reg-loongarch-lsx", lsxsize, lsxsize,
|
2024-01-25 16:32:35 +08:00
|
|
|
&loongarch_lsxregset, nullptr, cb_data);
|
2024-03-21 09:41:30 +08:00
|
|
|
cb (".reg-loongarch-lasx", lasxsize, lasxsize,
|
2024-01-25 16:32:35 +08:00
|
|
|
&loongarch_lasxregset, nullptr, cb_data);
|
2024-01-25 16:32:36 +08:00
|
|
|
cb (".reg-loongarch-lbt", lbtsize, lbtsize,
|
|
|
|
&loongarch_lbtregset, nullptr, 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
|
gdb: pass frames as `const frame_info_ptr &`
We currently pass frames to function by value, as `frame_info_ptr`.
This is somewhat expensive:
- the size of `frame_info_ptr` is 64 bytes, which is a bit big to pass
by value
- the constructors and destructor link/unlink the object in the global
`frame_info_ptr::frame_list` list. This is an `intrusive_list`, so
it's not so bad: it's just assigning a few points, there's no memory
allocation as if it was `std::list`, but still it's useless to do
that over and over.
As suggested by Tom Tromey, change many function signatures to accept
`const frame_info_ptr &` instead of `frame_info_ptr`.
Some functions reassign their `frame_info_ptr` parameter, like:
void
the_func (frame_info_ptr frame)
{
for (; frame != nullptr; frame = get_prev_frame (frame))
{
...
}
}
I wondered what to do about them, do I leave them as-is or change them
(and need to introduce a separate local variable that can be
re-assigned). I opted for the later for consistency. It might not be
clear why some functions take `const frame_info_ptr &` while others take
`frame_info_ptr`. Also, if a function took a `frame_info_ptr` because
it did re-assign its parameter, I doubt that we would think to change it
to `const frame_info_ptr &` should the implementation change such that
it doesn't need to take `frame_info_ptr` anymore. It seems better to
have a simple rule and apply it everywhere.
Change-Id: I59d10addef687d157f82ccf4d54f5dde9a963fd0
Approved-By: Andrew Burgess <aburgess@redhat.com>
2024-02-20 02:07:47 +08:00
|
|
|
loongarch_linux_syscall_next_pc (const frame_info_ptr &frame)
|
2022-06-25 09:21:16 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-31 16:10:30 +08:00
|
|
|
/* Implement the "get_syscall_number" gdbarch method. */
|
|
|
|
|
|
|
|
static LONGEST
|
|
|
|
loongarch_linux_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
|
|
|
|
{
|
|
|
|
struct regcache *regcache = get_thread_regcache (thread);
|
|
|
|
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
|
|
|
|
int regsize = register_size (gdbarch, LOONGARCH_A7_REGNUM);
|
|
|
|
/* The content of a register. */
|
|
|
|
gdb_byte buf[8];
|
|
|
|
/* The result. */
|
|
|
|
LONGEST ret;
|
|
|
|
|
|
|
|
gdb_assert (regsize <= sizeof (buf));
|
|
|
|
|
|
|
|
/* Getting the system call number from the register.
|
|
|
|
When dealing with the LoongArch architecture, this information
|
|
|
|
is stored at the a7 register. */
|
|
|
|
regcache->cooked_read (LOONGARCH_A7_REGNUM, buf);
|
|
|
|
|
|
|
|
ret = extract_signed_integer (buf, regsize, byte_order);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
gdb: move the type cast into gdbarch_tdep
I built GDB for all targets on a x86-64/GNU-Linux system, and
then (accidentally) passed GDB a RISC-V binary, and asked GDB to "run"
the binary on the native target. I got this error:
(gdb) show architecture
The target architecture is set to "auto" (currently "i386").
(gdb) file /tmp/hello.rv32.exe
Reading symbols from /tmp/hello.rv32.exe...
(gdb) show architecture
The target architecture is set to "auto" (currently "riscv:rv32").
(gdb) run
Starting program: /tmp/hello.rv32.exe
../../src/gdb/i387-tdep.c:596: internal-error: i387_supply_fxsave: Assertion `tdep->st0_regnum >= I386_ST0_REGNUM' failed.
What's going on here is this; initially the architecture is i386, this
is based on the default architecture, which is set based on the native
target. After loading the RISC-V executable the architecture of the
current inferior is updated based on the architecture of the
executable.
When we "run", GDB does a fork & exec, with the inferior being
controlled through ptrace. GDB sees an initial stop from the inferior
as soon as the inferior comes to life. In response to this stop GDB
ends up calling save_stop_reason (linux-nat.c), which ends up trying
to read register from the inferior, to do this we end up calling
target_ops::fetch_registers, which, for the x86-64 native target,
calls amd64_linux_nat_target::fetch_registers.
After this I eventually end up in i387_supply_fxsave, different x86
based targets will end in different functions to fetch registers, but
it doesn't really matter which function we end up in, the problem is
this line, which is repeated in many places:
i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (arch);
The problem here is that the ARCH in this line comes from the current
inferior, which, as we discussed above, will be a RISC-V gdbarch, the
tdep field will actually be of type riscv_gdbarch_tdep, not
i386_gdbarch_tdep. After this cast we are relying on undefined
behaviour, in my case I happen to trigger an assert, but this might
not always be the case.
The thing I tried that exposed this problem was of course, trying to
start an executable of the wrong architecture on a native target. I
don't think that the correct solution for this problem is to detect,
at the point of cast, that the gdbarch_tdep object is of the wrong
type, but, I did wonder, is there a way that we could protect
ourselves from incorrectly casting the gdbarch_tdep object?
I think that there is something we can do here, and this commit is the
first step in that direction, though no actual check is added by this
commit.
This commit can be split into two parts:
(1) In gdbarch.h and arch-utils.c. In these files I have modified
gdbarch_tdep (the function) so that it now takes a template argument,
like this:
template<typename TDepType>
static inline TDepType *
gdbarch_tdep (struct gdbarch *gdbarch)
{
struct gdbarch_tdep *tdep = gdbarch_tdep_1 (gdbarch);
return static_cast<TDepType *> (tdep);
}
After this change we are no better protected, but the cast is now
done within the gdbarch_tdep function rather than at the call sites,
this leads to the second, much larger change in this commit,
(2) Everywhere gdbarch_tdep is called, we make changes like this:
- i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (arch);
+ i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
There should be no functional change after this commit.
In the next commit I will build on this change to add an assertion in
gdbarch_tdep that checks we are casting to the correct type.
2022-05-19 20:20:17 +08:00
|
|
|
loongarch_gdbarch_tdep *tdep = gdbarch_tdep<loongarch_gdbarch_tdep> (gdbarch);
|
2022-06-25 09:21:16 +08:00
|
|
|
|
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;
|
2024-01-31 16:10:30 +08:00
|
|
|
|
2024-02-29 16:39:10 +08:00
|
|
|
/* Set the correct XML syscall filename. */
|
|
|
|
set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_LOONGARCH);
|
|
|
|
|
2024-01-31 16:10:30 +08:00
|
|
|
/* Get the syscall number from the arch's register. */
|
|
|
|
set_gdbarch_get_syscall_number (gdbarch, loongarch_linux_get_syscall_number);
|
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,
|
2023-03-10 03:56:09 +08:00
|
|
|
GDB_OSABI_LINUX, loongarch_linux_init_abi);
|
2022-02-11 20:15:06 +08:00
|
|
|
gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch64,
|
2023-03-10 03:56:09 +08:00
|
|
|
GDB_OSABI_LINUX, loongarch_linux_init_abi);
|
2022-02-11 20:15:06 +08:00
|
|
|
}
|