1*6881a400Schristos /* Target-dependent code for NetBSD/aarch64. 2*6881a400Schristos 3*6881a400Schristos Copyright (C) 2017-2018 Free Software Foundation, Inc. 4*6881a400Schristos 5*6881a400Schristos This file is part of GDB. 6*6881a400Schristos 7*6881a400Schristos This program is free software; you can redistribute it and/or modify 8*6881a400Schristos it under the terms of the GNU General Public License as published by 9*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 10*6881a400Schristos (at your option) any later version. 11*6881a400Schristos 12*6881a400Schristos This program is distributed in the hope that it will be useful, 13*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 14*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*6881a400Schristos GNU General Public License for more details. 16*6881a400Schristos 17*6881a400Schristos You should have received a copy of the GNU General Public License 18*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19*6881a400Schristos 20*6881a400Schristos #include "defs.h" 21*6881a400Schristos 22*6881a400Schristos #include "gdbarch.h" 23*6881a400Schristos #include "netbsd-tdep.h" 24*6881a400Schristos #include "aarch64-tdep.h" 25*6881a400Schristos #include "aarch64-netbsd-tdep.h" 26*6881a400Schristos #include "osabi.h" 27*6881a400Schristos #include "solib-svr4.h" 28*6881a400Schristos #include "target.h" 29*6881a400Schristos #include "tramp-frame.h" 30*6881a400Schristos #include "trad-frame.h" 31*6881a400Schristos 32*6881a400Schristos /* In a signal frame, sp points to a 'struct sigframe' which is 33*6881a400Schristos defined as: 34*6881a400Schristos 35*6881a400Schristos struct sigframe { 36*6881a400Schristos siginfo_t sf_si; 37*6881a400Schristos ucontext_t sf_uc; 38*6881a400Schristos }; 39*6881a400Schristos 40*6881a400Schristos ucontext_t is defined as: 41*6881a400Schristos 42*6881a400Schristos struct __ucontext { 43*6881a400Schristos sigset_t uc_sigmask; 44*6881a400Schristos mcontext_t uc_mcontext; 45*6881a400Schristos ... 46*6881a400Schristos }; 47*6881a400Schristos 48*6881a400Schristos The mcontext_t contains the general purpose register set followed 49*6881a400Schristos by the floating point register set. The floating point register 50*6881a400Schristos set is only valid if the _MC_FP_VALID flag is set in mc_flags. */ 51*6881a400Schristos 52*6881a400Schristos #define AARCH64_MCONTEXT_REG_SIZE 8 53*6881a400Schristos #define AARCH64_MCONTEXT_FPREG_SIZE 16 54*6881a400Schristos #define AARCH64_SIGFRAME_UCONTEXT_OFFSET 80 55*6881a400Schristos #define AARCH64_UCONTEXT_MCONTEXT_OFFSET 16 56*6881a400Schristos #define AARCH64_MCONTEXT_FPREGS_OFFSET 272 57*6881a400Schristos #define AARCH64_MCONTEXT_FLAGS_OFFSET 800 58*6881a400Schristos #define AARCH64_MCONTEXT_FLAG_FP_VALID 0x1 59*6881a400Schristos 60*6881a400Schristos /* Implement the "init" method of struct tramp_frame. */ 61*6881a400Schristos 62*6881a400Schristos static void 63*6881a400Schristos aarch64_nbsd_sigframe_init (const struct tramp_frame *self, 64*6881a400Schristos frame_info_ptr this_frame, 65*6881a400Schristos struct trad_frame_cache *this_cache, 66*6881a400Schristos CORE_ADDR func) 67*6881a400Schristos { 68*6881a400Schristos struct gdbarch *gdbarch = get_frame_arch (this_frame); 69*6881a400Schristos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 70*6881a400Schristos CORE_ADDR sp = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM); 71*6881a400Schristos CORE_ADDR mcontext_addr = 72*6881a400Schristos sp 73*6881a400Schristos + AARCH64_SIGFRAME_UCONTEXT_OFFSET 74*6881a400Schristos + AARCH64_UCONTEXT_MCONTEXT_OFFSET; 75*6881a400Schristos gdb_byte buf[4]; 76*6881a400Schristos int i; 77*6881a400Schristos 78*6881a400Schristos for (i = 0; i < 30; i++) 79*6881a400Schristos { 80*6881a400Schristos trad_frame_set_reg_addr (this_cache, 81*6881a400Schristos AARCH64_X0_REGNUM + i, 82*6881a400Schristos mcontext_addr + i * AARCH64_MCONTEXT_REG_SIZE); 83*6881a400Schristos } 84*6881a400Schristos trad_frame_set_reg_addr (this_cache, AARCH64_LR_REGNUM, 85*6881a400Schristos mcontext_addr + 30 * AARCH64_MCONTEXT_REG_SIZE); 86*6881a400Schristos trad_frame_set_reg_addr (this_cache, AARCH64_SP_REGNUM, 87*6881a400Schristos mcontext_addr + 31 * AARCH64_MCONTEXT_REG_SIZE); 88*6881a400Schristos trad_frame_set_reg_addr (this_cache, AARCH64_PC_REGNUM, 89*6881a400Schristos mcontext_addr + 32 * AARCH64_MCONTEXT_REG_SIZE); 90*6881a400Schristos trad_frame_set_reg_addr (this_cache, AARCH64_CPSR_REGNUM, 91*6881a400Schristos mcontext_addr + 33 * AARCH64_MCONTEXT_REG_SIZE); 92*6881a400Schristos 93*6881a400Schristos if (target_read_memory (mcontext_addr + AARCH64_MCONTEXT_FLAGS_OFFSET, buf, 94*6881a400Schristos 4) == 0 95*6881a400Schristos && (extract_unsigned_integer (buf, 4, byte_order) 96*6881a400Schristos & AARCH64_MCONTEXT_FLAG_FP_VALID)) 97*6881a400Schristos { 98*6881a400Schristos for (i = 0; i < 32; i++) 99*6881a400Schristos { 100*6881a400Schristos trad_frame_set_reg_addr (this_cache, AARCH64_V0_REGNUM + i, 101*6881a400Schristos mcontext_addr 102*6881a400Schristos + AARCH64_MCONTEXT_FPREGS_OFFSET 103*6881a400Schristos + i * AARCH64_MCONTEXT_FPREG_SIZE); 104*6881a400Schristos } 105*6881a400Schristos trad_frame_set_reg_addr (this_cache, AARCH64_FPSR_REGNUM, 106*6881a400Schristos mcontext_addr + AARCH64_MCONTEXT_FPREGS_OFFSET 107*6881a400Schristos + 32 * AARCH64_MCONTEXT_FPREG_SIZE); 108*6881a400Schristos trad_frame_set_reg_addr (this_cache, AARCH64_FPCR_REGNUM, 109*6881a400Schristos mcontext_addr + AARCH64_MCONTEXT_FPREGS_OFFSET 110*6881a400Schristos + 32 * AARCH64_MCONTEXT_FPREG_SIZE + 4); 111*6881a400Schristos } 112*6881a400Schristos 113*6881a400Schristos trad_frame_set_id (this_cache, frame_id_build (sp, func)); 114*6881a400Schristos } 115*6881a400Schristos 116*6881a400Schristos static const struct tramp_frame aarch64_nbsd_sigframe = 117*6881a400Schristos { 118*6881a400Schristos SIGTRAMP_FRAME, 119*6881a400Schristos 4, 120*6881a400Schristos { 121*6881a400Schristos {0x910003e0, ULONGEST_MAX}, /* mov x0, sp */ 122*6881a400Schristos {0x91014000, ULONGEST_MAX}, /* add x0, x0, #SF_UC */ 123*6881a400Schristos {0xd2803428, ULONGEST_MAX}, /* mov x8, #SYS_sigreturn */ 124*6881a400Schristos {0xd4000001, ULONGEST_MAX}, /* svc 0x0 */ 125*6881a400Schristos {(ULONGEST)TRAMP_SENTINEL_INSN, ULONGEST_MAX} 126*6881a400Schristos }, 127*6881a400Schristos aarch64_nbsd_sigframe_init 128*6881a400Schristos }; 129*6881a400Schristos 130*6881a400Schristos /* Register maps. */ 131*6881a400Schristos 132*6881a400Schristos static const struct regcache_map_entry aarch64_nbsd_gregmap[] = 133*6881a400Schristos { 134*6881a400Schristos { 30, AARCH64_X0_REGNUM, 8 }, /* x0 ... x29 */ 135*6881a400Schristos { 1, AARCH64_LR_REGNUM, 8 }, 136*6881a400Schristos { 1, AARCH64_SP_REGNUM, 8 }, 137*6881a400Schristos { 1, AARCH64_PC_REGNUM, 8 }, 138*6881a400Schristos { 1, AARCH64_CPSR_REGNUM, 4 }, 139*6881a400Schristos { 0 } 140*6881a400Schristos }; 141*6881a400Schristos 142*6881a400Schristos static const struct regcache_map_entry aarch64_nbsd_fpregmap[] = 143*6881a400Schristos { 144*6881a400Schristos { 32, AARCH64_V0_REGNUM, 16 }, /* v0 ... v31 */ 145*6881a400Schristos { 1, AARCH64_FPSR_REGNUM, 4 }, 146*6881a400Schristos { 1, AARCH64_FPCR_REGNUM, 4 }, 147*6881a400Schristos { 0 } 148*6881a400Schristos }; 149*6881a400Schristos 150*6881a400Schristos /* Register set definitions. */ 151*6881a400Schristos 152*6881a400Schristos const struct regset aarch64_nbsd_gregset = 153*6881a400Schristos { 154*6881a400Schristos aarch64_nbsd_gregmap, 155*6881a400Schristos regcache_supply_regset, regcache_collect_regset 156*6881a400Schristos }; 157*6881a400Schristos 158*6881a400Schristos const struct regset aarch64_nbsd_fpregset = 159*6881a400Schristos { 160*6881a400Schristos aarch64_nbsd_fpregmap, 161*6881a400Schristos regcache_supply_regset, regcache_collect_regset 162*6881a400Schristos }; 163*6881a400Schristos 164*6881a400Schristos /* Implement the "regset_from_core_section" gdbarch method. */ 165*6881a400Schristos 166*6881a400Schristos static void 167*6881a400Schristos aarch64_nbsd_iterate_over_regset_sections (struct gdbarch *gdbarch, 168*6881a400Schristos iterate_over_regset_sections_cb *cb, 169*6881a400Schristos void *cb_data, 170*6881a400Schristos const struct regcache *regcache) 171*6881a400Schristos { 172*6881a400Schristos cb (".reg", AARCH64_NBSD_SIZEOF_GREGSET, AARCH64_NBSD_SIZEOF_GREGSET, 173*6881a400Schristos &aarch64_nbsd_gregset, NULL, cb_data); 174*6881a400Schristos cb (".reg2", AARCH64_NBSD_SIZEOF_FPREGSET, AARCH64_NBSD_SIZEOF_FPREGSET, 175*6881a400Schristos &aarch64_nbsd_fpregset, NULL, cb_data); 176*6881a400Schristos } 177*6881a400Schristos 178*6881a400Schristos /* Implement the 'init_osabi' method of struct gdb_osabi_handler. */ 179*6881a400Schristos 180*6881a400Schristos static void 181*6881a400Schristos aarch64_nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) 182*6881a400Schristos { 183*6881a400Schristos aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep>(gdbarch); 184*6881a400Schristos 185*6881a400Schristos nbsd_init_abi (info, gdbarch); 186*6881a400Schristos 187*6881a400Schristos set_solib_svr4_fetch_link_map_offsets (gdbarch, 188*6881a400Schristos svr4_lp64_fetch_link_map_offsets); 189*6881a400Schristos 190*6881a400Schristos tramp_frame_prepend_unwinder (gdbarch, &aarch64_nbsd_sigframe); 191*6881a400Schristos 192*6881a400Schristos /* Enable longjmp. */ 193*6881a400Schristos tdep->jb_pc = 13; 194*6881a400Schristos 195*6881a400Schristos set_gdbarch_iterate_over_regset_sections 196*6881a400Schristos (gdbarch, aarch64_nbsd_iterate_over_regset_sections); 197*6881a400Schristos } 198*6881a400Schristos 199*6881a400Schristos void 200*6881a400Schristos _initialize_aarch64_nbsd_tdep () 201*6881a400Schristos { 202*6881a400Schristos gdbarch_register_osabi (bfd_arch_aarch64, 0, GDB_OSABI_NETBSD, 203*6881a400Schristos aarch64_nbsd_init_abi); 204*6881a400Schristos } 205