18dffb485Schristos /* Target dependent code for GDB on TI C6x systems. 28dffb485Schristos 3*f1c2b495Schristos Copyright (C) 2010-2024 Free Software Foundation, Inc. 48dffb485Schristos Contributed by Andrew Jenner <andrew@codesourcery.com> 58dffb485Schristos Contributed by Yao Qi <yao@codesourcery.com> 68dffb485Schristos 78dffb485Schristos This file is part of GDB. 88dffb485Schristos 98dffb485Schristos This program is free software; you can redistribute it and/or modify 108dffb485Schristos it under the terms of the GNU General Public License as published by 118dffb485Schristos the Free Software Foundation; either version 3 of the License, or 128dffb485Schristos (at your option) any later version. 138dffb485Schristos 148dffb485Schristos This program is distributed in the hope that it will be useful, 158dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 168dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 178dffb485Schristos GNU General Public License for more details. 188dffb485Schristos 198dffb485Schristos You should have received a copy of the GNU General Public License 208dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 218dffb485Schristos 228dffb485Schristos #include "linux-low.h" 238dffb485Schristos #include "arch/tic6x.h" 248dffb485Schristos #include "tdesc.h" 258dffb485Schristos 268dffb485Schristos #include "nat/gdb_ptrace.h" 278dffb485Schristos #include <endian.h> 288dffb485Schristos 298dffb485Schristos #include "gdb_proc_service.h" 308dffb485Schristos 318dffb485Schristos #ifndef PTRACE_GET_THREAD_AREA 328dffb485Schristos #define PTRACE_GET_THREAD_AREA 25 338dffb485Schristos #endif 348dffb485Schristos 358dffb485Schristos /* There are at most 69 registers accessible in ptrace. */ 368dffb485Schristos #define TIC6X_NUM_REGS 69 378dffb485Schristos 388dffb485Schristos #include <asm/ptrace.h> 398dffb485Schristos 408dffb485Schristos /* Linux target op definitions for the TI C6x architecture. */ 418dffb485Schristos 428dffb485Schristos class tic6x_target : public linux_process_target 438dffb485Schristos { 448dffb485Schristos public: 458dffb485Schristos 468dffb485Schristos const regs_info *get_regs_info () override; 478dffb485Schristos 488dffb485Schristos const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override; 498dffb485Schristos 508dffb485Schristos protected: 518dffb485Schristos 528dffb485Schristos void low_arch_setup () override; 538dffb485Schristos 548dffb485Schristos bool low_cannot_fetch_register (int regno) override; 558dffb485Schristos 568dffb485Schristos bool low_cannot_store_register (int regno) override; 578dffb485Schristos 588dffb485Schristos bool low_supports_breakpoints () override; 598dffb485Schristos 608dffb485Schristos CORE_ADDR low_get_pc (regcache *regcache) override; 618dffb485Schristos 628dffb485Schristos void low_set_pc (regcache *regcache, CORE_ADDR newpc) override; 638dffb485Schristos 648dffb485Schristos bool low_breakpoint_at (CORE_ADDR pc) override; 658dffb485Schristos }; 668dffb485Schristos 678dffb485Schristos /* The singleton target ops object. */ 688dffb485Schristos 698dffb485Schristos static tic6x_target the_tic6x_target; 708dffb485Schristos 718dffb485Schristos /* Defined in auto-generated file tic6x-c64xp-linux.c. */ 728dffb485Schristos void init_registers_tic6x_c64xp_linux (void); 738dffb485Schristos extern const struct target_desc *tdesc_tic6x_c64xp_linux; 748dffb485Schristos 758dffb485Schristos /* Defined in auto-generated file tic6x-c64x-linux.c. */ 768dffb485Schristos void init_registers_tic6x_c64x_linux (void); 778dffb485Schristos extern const struct target_desc *tdesc_tic6x_c64x_linux; 788dffb485Schristos 798dffb485Schristos /* Defined in auto-generated file tic62x-c6xp-linux.c. */ 808dffb485Schristos void init_registers_tic6x_c62x_linux (void); 818dffb485Schristos extern const struct target_desc *tdesc_tic6x_c62x_linux; 828dffb485Schristos 838dffb485Schristos union tic6x_register 848dffb485Schristos { 858dffb485Schristos unsigned char buf[4]; 868dffb485Schristos 878dffb485Schristos int reg32; 888dffb485Schristos }; 898dffb485Schristos 908dffb485Schristos /* Return the ptrace ``address'' of register REGNO. */ 918dffb485Schristos 928dffb485Schristos #if __BYTE_ORDER == __BIG_ENDIAN 938dffb485Schristos static int tic6x_regmap_c64xp[] = { 948dffb485Schristos /* A0 - A15 */ 958dffb485Schristos 53, 52, 55, 54, 57, 56, 59, 58, 968dffb485Schristos 61, 60, 63, 62, 65, 64, 67, 66, 978dffb485Schristos /* B0 - B15 */ 988dffb485Schristos 23, 22, 25, 24, 27, 26, 29, 28, 998dffb485Schristos 31, 30, 33, 32, 35, 34, 69, 68, 1008dffb485Schristos /* CSR PC */ 1018dffb485Schristos 5, 4, 1028dffb485Schristos /* A16 - A31 */ 1038dffb485Schristos 37, 36, 39, 38, 41, 40, 43, 42, 1048dffb485Schristos 45, 44, 47, 46, 49, 48, 51, 50, 1058dffb485Schristos /* B16 - B31 */ 1068dffb485Schristos 7, 6, 9, 8, 11, 10, 13, 12, 1078dffb485Schristos 15, 14, 17, 16, 19, 18, 21, 20, 1088dffb485Schristos /* TSR, ILC, RILC */ 1098dffb485Schristos 1, 2, 3 1108dffb485Schristos }; 1118dffb485Schristos 1128dffb485Schristos static int tic6x_regmap_c64x[] = { 1138dffb485Schristos /* A0 - A15 */ 1148dffb485Schristos 51, 50, 53, 52, 55, 54, 57, 56, 1158dffb485Schristos 59, 58, 61, 60, 63, 62, 65, 64, 1168dffb485Schristos /* B0 - B15 */ 1178dffb485Schristos 21, 20, 23, 22, 25, 24, 27, 26, 1188dffb485Schristos 29, 28, 31, 30, 33, 32, 67, 66, 1198dffb485Schristos /* CSR PC */ 1208dffb485Schristos 3, 2, 1218dffb485Schristos /* A16 - A31 */ 1228dffb485Schristos 35, 34, 37, 36, 39, 38, 41, 40, 1238dffb485Schristos 43, 42, 45, 44, 47, 46, 49, 48, 1248dffb485Schristos /* B16 - B31 */ 1258dffb485Schristos 5, 4, 7, 6, 9, 8, 11, 10, 1268dffb485Schristos 13, 12, 15, 14, 17, 16, 19, 18, 1278dffb485Schristos -1, -1, -1 1288dffb485Schristos }; 1298dffb485Schristos 1308dffb485Schristos static int tic6x_regmap_c62x[] = { 1318dffb485Schristos /* A0 - A15 */ 1328dffb485Schristos 19, 18, 21, 20, 23, 22, 25, 24, 1338dffb485Schristos 27, 26, 29, 28, 31, 30, 33, 32, 1348dffb485Schristos /* B0 - B15 */ 1358dffb485Schristos 5, 4, 7, 6, 9, 8, 11, 10, 1368dffb485Schristos 13, 12, 15, 14, 17, 16, 35, 34, 1378dffb485Schristos /* CSR, PC */ 1388dffb485Schristos 3, 2, 1398dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1408dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1418dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1428dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1438dffb485Schristos -1, -1, -1 1448dffb485Schristos }; 1458dffb485Schristos 1468dffb485Schristos #else 1478dffb485Schristos static int tic6x_regmap_c64xp[] = { 1488dffb485Schristos /* A0 - A15 */ 1498dffb485Schristos 52, 53, 54, 55, 56, 57, 58, 59, 1508dffb485Schristos 60, 61, 62, 63, 64, 65, 66, 67, 1518dffb485Schristos /* B0 - B15 */ 1528dffb485Schristos 22, 23, 24, 25, 26, 27, 28, 29, 1538dffb485Schristos 30, 31, 32, 33, 34, 35, 68, 69, 1548dffb485Schristos /* CSR PC */ 1558dffb485Schristos 4, 5, 1568dffb485Schristos /* A16 - A31 */ 1578dffb485Schristos 36, 37, 38, 39, 40, 41, 42, 43, 1588dffb485Schristos 44, 45, 46, 47, 48, 49, 50, 51, 1598dffb485Schristos /* B16 -B31 */ 1608dffb485Schristos 6, 7, 8, 9, 10, 11, 12, 13, 1618dffb485Schristos 14, 15, 16, 17, 18, 19, 20, 31, 1628dffb485Schristos /* TSR, ILC, RILC */ 1638dffb485Schristos 0, 3, 2 1648dffb485Schristos }; 1658dffb485Schristos 1668dffb485Schristos static int tic6x_regmap_c64x[] = { 1678dffb485Schristos /* A0 - A15 */ 1688dffb485Schristos 50, 51, 52, 53, 54, 55, 56, 57, 1698dffb485Schristos 58, 59, 60, 61, 62, 63, 64, 65, 1708dffb485Schristos /* B0 - B15 */ 1718dffb485Schristos 20, 21, 22, 23, 24, 25, 26, 27, 1728dffb485Schristos 28, 29, 30, 31, 32, 33, 66, 67, 1738dffb485Schristos /* CSR PC */ 1748dffb485Schristos 2, 3, 1758dffb485Schristos /* A16 - A31 */ 1768dffb485Schristos 34, 35, 36, 37, 38, 39, 40, 41, 1778dffb485Schristos 42, 43, 44, 45, 46, 47, 48, 49, 1788dffb485Schristos /* B16 - B31 */ 1798dffb485Schristos 4, 5, 6, 7, 8, 9, 10, 11, 1808dffb485Schristos 12, 13, 14, 15, 16, 17, 18, 19, 1818dffb485Schristos -1, -1, -1 1828dffb485Schristos }; 1838dffb485Schristos 1848dffb485Schristos static int tic6x_regmap_c62x[] = { 1858dffb485Schristos /* A0 - A15 */ 1868dffb485Schristos 18, 19, 20, 21, 22, 23, 24, 25, 1878dffb485Schristos 26, 27, 28, 29, 30, 31, 32, 33, 1888dffb485Schristos /* B0 - B15 */ 1898dffb485Schristos 4, 5, 6, 7, 8, 9, 10, 11, 1908dffb485Schristos 12, 13, 14, 15, 16, 17, 34, 35, 1918dffb485Schristos /* CSR PC */ 1928dffb485Schristos 2, 3, 1938dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1948dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1958dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1968dffb485Schristos -1, -1, -1, -1, -1, -1, -1, -1, 1978dffb485Schristos -1, -1, -1 1988dffb485Schristos }; 1998dffb485Schristos 2008dffb485Schristos #endif 2018dffb485Schristos 2028dffb485Schristos static int *tic6x_regmap; 2038dffb485Schristos static unsigned int tic6x_breakpoint; 2048dffb485Schristos #define tic6x_breakpoint_len 4 2058dffb485Schristos 2068dffb485Schristos /* Implementation of target ops method "sw_breakpoint_from_kind". */ 2078dffb485Schristos 2088dffb485Schristos const gdb_byte * 2098dffb485Schristos tic6x_target::sw_breakpoint_from_kind (int kind, int *size) 2108dffb485Schristos { 2118dffb485Schristos *size = tic6x_breakpoint_len; 2128dffb485Schristos return (const gdb_byte *) &tic6x_breakpoint; 2138dffb485Schristos } 2148dffb485Schristos 2158dffb485Schristos static struct usrregs_info tic6x_usrregs_info = 2168dffb485Schristos { 2178dffb485Schristos TIC6X_NUM_REGS, 2188dffb485Schristos NULL, /* Set in tic6x_read_description. */ 2198dffb485Schristos }; 2208dffb485Schristos 2218dffb485Schristos static const struct target_desc * 2228dffb485Schristos tic6x_read_description (enum c6x_feature feature) 2238dffb485Schristos { 2248dffb485Schristos static target_desc *tdescs[C6X_LAST] = { }; 2258dffb485Schristos struct target_desc **tdesc = &tdescs[feature]; 2268dffb485Schristos 2278dffb485Schristos if (*tdesc == NULL) 2288dffb485Schristos { 2298dffb485Schristos *tdesc = tic6x_create_target_description (feature); 2308dffb485Schristos static const char *expedite_regs[] = { "A15", "PC", NULL }; 2318dffb485Schristos init_target_desc (*tdesc, expedite_regs); 2328dffb485Schristos } 2338dffb485Schristos 2348dffb485Schristos return *tdesc; 2358dffb485Schristos } 2368dffb485Schristos 2378dffb485Schristos bool 2388dffb485Schristos tic6x_target::low_cannot_fetch_register (int regno) 2398dffb485Schristos { 2408dffb485Schristos return (tic6x_regmap[regno] == -1); 2418dffb485Schristos } 2428dffb485Schristos 2438dffb485Schristos bool 2448dffb485Schristos tic6x_target::low_cannot_store_register (int regno) 2458dffb485Schristos { 2468dffb485Schristos return (tic6x_regmap[regno] == -1); 2478dffb485Schristos } 2488dffb485Schristos 2498dffb485Schristos bool 2508dffb485Schristos tic6x_target::low_supports_breakpoints () 2518dffb485Schristos { 2528dffb485Schristos return true; 2538dffb485Schristos } 2548dffb485Schristos 2558dffb485Schristos CORE_ADDR 2568dffb485Schristos tic6x_target::low_get_pc (regcache *regcache) 2578dffb485Schristos { 2588dffb485Schristos union tic6x_register pc; 2598dffb485Schristos 2608dffb485Schristos collect_register_by_name (regcache, "PC", pc.buf); 2618dffb485Schristos return pc.reg32; 2628dffb485Schristos } 2638dffb485Schristos 2648dffb485Schristos void 2658dffb485Schristos tic6x_target::low_set_pc (regcache *regcache, CORE_ADDR pc) 2668dffb485Schristos { 2678dffb485Schristos union tic6x_register newpc; 2688dffb485Schristos 2698dffb485Schristos newpc.reg32 = pc; 2708dffb485Schristos supply_register_by_name (regcache, "PC", newpc.buf); 2718dffb485Schristos } 2728dffb485Schristos 2738dffb485Schristos bool 2748dffb485Schristos tic6x_target::low_breakpoint_at (CORE_ADDR where) 2758dffb485Schristos { 2768dffb485Schristos unsigned int insn; 2778dffb485Schristos 2788dffb485Schristos read_memory (where, (unsigned char *) &insn, 4); 2798dffb485Schristos if (insn == tic6x_breakpoint) 2808dffb485Schristos return true; 2818dffb485Schristos 2828dffb485Schristos /* If necessary, recognize more trap instructions here. GDB only uses the 2838dffb485Schristos one. */ 2848dffb485Schristos return false; 2858dffb485Schristos } 2868dffb485Schristos 2878dffb485Schristos /* Fetch the thread-local storage pointer for libthread_db. */ 2888dffb485Schristos 2898dffb485Schristos ps_err_e 2908dffb485Schristos ps_get_thread_area (struct ps_prochandle *ph, 2918dffb485Schristos lwpid_t lwpid, int idx, void **base) 2928dffb485Schristos { 2938dffb485Schristos if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0) 2948dffb485Schristos return PS_ERR; 2958dffb485Schristos 2968dffb485Schristos /* IDX is the bias from the thread pointer to the beginning of the 2978dffb485Schristos thread descriptor. It has to be subtracted due to implementation 2988dffb485Schristos quirks in libthread_db. */ 2998dffb485Schristos *base = (void *) ((char *) *base - idx); 3008dffb485Schristos 3018dffb485Schristos return PS_OK; 3028dffb485Schristos } 3038dffb485Schristos 3048dffb485Schristos static void 3058dffb485Schristos tic6x_collect_register (struct regcache *regcache, int regno, 3068dffb485Schristos union tic6x_register *reg) 3078dffb485Schristos { 3088dffb485Schristos union tic6x_register tmp_reg; 3098dffb485Schristos 3108dffb485Schristos collect_register (regcache, regno, &tmp_reg.reg32); 3118dffb485Schristos reg->reg32 = tmp_reg.reg32; 3128dffb485Schristos } 3138dffb485Schristos 3148dffb485Schristos static void 3158dffb485Schristos tic6x_supply_register (struct regcache *regcache, int regno, 3168dffb485Schristos const union tic6x_register *reg) 3178dffb485Schristos { 3188dffb485Schristos int offset = 0; 3198dffb485Schristos 3208dffb485Schristos supply_register (regcache, regno, reg->buf + offset); 3218dffb485Schristos } 3228dffb485Schristos 3238dffb485Schristos static void 3248dffb485Schristos tic6x_fill_gregset (struct regcache *regcache, void *buf) 3258dffb485Schristos { 3268dffb485Schristos auto regset = static_cast<union tic6x_register *> (buf); 3278dffb485Schristos int i; 3288dffb485Schristos 3298dffb485Schristos for (i = 0; i < TIC6X_NUM_REGS; i++) 3308dffb485Schristos if (tic6x_regmap[i] != -1) 3318dffb485Schristos tic6x_collect_register (regcache, i, regset + tic6x_regmap[i]); 3328dffb485Schristos } 3338dffb485Schristos 3348dffb485Schristos static void 3358dffb485Schristos tic6x_store_gregset (struct regcache *regcache, const void *buf) 3368dffb485Schristos { 3378dffb485Schristos const auto regset = static_cast<const union tic6x_register *> (buf); 3388dffb485Schristos int i; 3398dffb485Schristos 3408dffb485Schristos for (i = 0; i < TIC6X_NUM_REGS; i++) 3418dffb485Schristos if (tic6x_regmap[i] != -1) 3428dffb485Schristos tic6x_supply_register (regcache, i, regset + tic6x_regmap[i]); 3438dffb485Schristos } 3448dffb485Schristos 3458dffb485Schristos static struct regset_info tic6x_regsets[] = { 3468dffb485Schristos { PTRACE_GETREGS, PTRACE_SETREGS, 0, TIC6X_NUM_REGS * 4, GENERAL_REGS, 3478dffb485Schristos tic6x_fill_gregset, tic6x_store_gregset }, 3488dffb485Schristos NULL_REGSET 3498dffb485Schristos }; 3508dffb485Schristos 3518dffb485Schristos void 3528dffb485Schristos tic6x_target::low_arch_setup () 3538dffb485Schristos { 3548dffb485Schristos register unsigned int csr asm ("B2"); 3558dffb485Schristos unsigned int cpuid; 3568dffb485Schristos enum c6x_feature feature = C6X_CORE; 3578dffb485Schristos 3588dffb485Schristos /* Determine the CPU we're running on to find the register order. */ 3598dffb485Schristos __asm__ ("MVC .S2 CSR,%0" : "=r" (csr) :); 3608dffb485Schristos cpuid = csr >> 24; 3618dffb485Schristos switch (cpuid) 3628dffb485Schristos { 3638dffb485Schristos case 0x00: /* C62x */ 3648dffb485Schristos case 0x02: /* C67x */ 3658dffb485Schristos tic6x_regmap = tic6x_regmap_c62x; 3668dffb485Schristos tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */ 3678dffb485Schristos feature = C6X_CORE; 3688dffb485Schristos break; 3698dffb485Schristos case 0x03: /* C67x+ */ 3708dffb485Schristos tic6x_regmap = tic6x_regmap_c64x; 3718dffb485Schristos tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */ 3728dffb485Schristos feature = C6X_GP; 3738dffb485Schristos break; 3748dffb485Schristos case 0x0c: /* C64x */ 3758dffb485Schristos tic6x_regmap = tic6x_regmap_c64x; 3768dffb485Schristos tic6x_breakpoint = 0x0000a122; /* BNOP .S2 0,5 */ 3778dffb485Schristos feature = C6X_GP; 3788dffb485Schristos break; 3798dffb485Schristos case 0x10: /* C64x+ */ 3808dffb485Schristos case 0x14: /* C674x */ 3818dffb485Schristos case 0x15: /* C66x */ 3828dffb485Schristos tic6x_regmap = tic6x_regmap_c64xp; 3838dffb485Schristos tic6x_breakpoint = 0x56454314; /* illegal opcode */ 3848dffb485Schristos feature = C6X_C6XP; 3858dffb485Schristos break; 3868dffb485Schristos default: 3878dffb485Schristos error ("Unknown CPU ID 0x%02x", cpuid); 3888dffb485Schristos } 3898dffb485Schristos tic6x_usrregs_info.regmap = tic6x_regmap; 3908dffb485Schristos 3918dffb485Schristos current_process ()->tdesc = tic6x_read_description (feature); 3928dffb485Schristos } 3938dffb485Schristos 3948dffb485Schristos static struct regsets_info tic6x_regsets_info = 3958dffb485Schristos { 3968dffb485Schristos tic6x_regsets, /* regsets */ 3978dffb485Schristos 0, /* num_regsets */ 3988dffb485Schristos NULL, /* disabled_regsets */ 3998dffb485Schristos }; 4008dffb485Schristos 4018dffb485Schristos static struct regs_info myregs_info = 4028dffb485Schristos { 4038dffb485Schristos NULL, /* regset_bitmap */ 4048dffb485Schristos &tic6x_usrregs_info, 4058dffb485Schristos &tic6x_regsets_info 4068dffb485Schristos }; 4078dffb485Schristos 4088dffb485Schristos const regs_info * 4098dffb485Schristos tic6x_target::get_regs_info () 4108dffb485Schristos { 4118dffb485Schristos return &myregs_info; 4128dffb485Schristos } 4138dffb485Schristos 4148dffb485Schristos #if GDB_SELF_TEST 4158dffb485Schristos #include "gdbsupport/selftest.h" 4168dffb485Schristos 4178dffb485Schristos namespace selftests { 4188dffb485Schristos namespace tdesc { 4198dffb485Schristos static void 4208dffb485Schristos tic6x_tdesc_test () 4218dffb485Schristos { 4228dffb485Schristos SELF_CHECK (*tdesc_tic6x_c62x_linux == *tic6x_read_description (C6X_CORE)); 4238dffb485Schristos SELF_CHECK (*tdesc_tic6x_c64x_linux == *tic6x_read_description (C6X_GP)); 4248dffb485Schristos SELF_CHECK (*tdesc_tic6x_c64xp_linux == *tic6x_read_description (C6X_C6XP)); 4258dffb485Schristos } 4268dffb485Schristos } 4278dffb485Schristos } 4288dffb485Schristos #endif 4298dffb485Schristos 4308dffb485Schristos /* The linux target ops object. */ 4318dffb485Schristos 4328dffb485Schristos linux_process_target *the_linux_target = &the_tic6x_target; 4338dffb485Schristos 4348dffb485Schristos void 4358dffb485Schristos initialize_low_arch (void) 4368dffb485Schristos { 4378dffb485Schristos #if GDB_SELF_TEST 4388dffb485Schristos /* Initialize the Linux target descriptions. */ 4398dffb485Schristos init_registers_tic6x_c64xp_linux (); 4408dffb485Schristos init_registers_tic6x_c64x_linux (); 4418dffb485Schristos init_registers_tic6x_c62x_linux (); 4428dffb485Schristos 4438dffb485Schristos selftests::register_test ("tic6x-tdesc", selftests::tdesc::tic6x_tdesc_test); 4448dffb485Schristos #endif 4458dffb485Schristos 4468dffb485Schristos initialize_regsets_info (&tic6x_regsets_info); 4478dffb485Schristos } 448