tilegx-tdep.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /* Target-dependent code for the Tilera TILE-Gx processor.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "frame-base.h"
  17. #include "frame-unwind.h"
  18. #include "dwarf2/frame.h"
  19. #include "trad-frame.h"
  20. #include "symtab.h"
  21. #include "gdbtypes.h"
  22. #include "gdbcmd.h"
  23. #include "gdbcore.h"
  24. #include "value.h"
  25. #include "dis-asm.h"
  26. #include "inferior.h"
  27. #include "arch-utils.h"
  28. #include "regcache.h"
  29. #include "regset.h"
  30. #include "osabi.h"
  31. #include "linux-tdep.h"
  32. #include "objfiles.h"
  33. #include "solib-svr4.h"
  34. #include "tilegx-tdep.h"
  35. #include "opcode/tilegx.h"
  36. #include <algorithm>
  37. #include "gdbsupport/byte-vector.h"
  38. struct tilegx_frame_cache
  39. {
  40. /* Base address. */
  41. CORE_ADDR base;
  42. /* Function start. */
  43. CORE_ADDR start_pc;
  44. /* Table of saved registers. */
  45. trad_frame_saved_reg *saved_regs;
  46. };
  47. /* Register state values used by analyze_prologue. */
  48. enum reverse_state
  49. {
  50. REVERSE_STATE_REGISTER,
  51. REVERSE_STATE_VALUE,
  52. REVERSE_STATE_UNKNOWN
  53. };
  54. /* Register state used by analyze_prologue(). */
  55. struct tilegx_reverse_regs
  56. {
  57. LONGEST value;
  58. enum reverse_state state;
  59. };
  60. static const struct tilegx_reverse_regs
  61. template_reverse_regs[TILEGX_NUM_PHYS_REGS] =
  62. {
  63. { TILEGX_R0_REGNUM, REVERSE_STATE_REGISTER },
  64. { TILEGX_R1_REGNUM, REVERSE_STATE_REGISTER },
  65. { TILEGX_R2_REGNUM, REVERSE_STATE_REGISTER },
  66. { TILEGX_R3_REGNUM, REVERSE_STATE_REGISTER },
  67. { TILEGX_R4_REGNUM, REVERSE_STATE_REGISTER },
  68. { TILEGX_R5_REGNUM, REVERSE_STATE_REGISTER },
  69. { TILEGX_R6_REGNUM, REVERSE_STATE_REGISTER },
  70. { TILEGX_R7_REGNUM, REVERSE_STATE_REGISTER },
  71. { TILEGX_R8_REGNUM, REVERSE_STATE_REGISTER },
  72. { TILEGX_R9_REGNUM, REVERSE_STATE_REGISTER },
  73. { TILEGX_R10_REGNUM, REVERSE_STATE_REGISTER },
  74. { TILEGX_R11_REGNUM, REVERSE_STATE_REGISTER },
  75. { TILEGX_R12_REGNUM, REVERSE_STATE_REGISTER },
  76. { TILEGX_R13_REGNUM, REVERSE_STATE_REGISTER },
  77. { TILEGX_R14_REGNUM, REVERSE_STATE_REGISTER },
  78. { TILEGX_R15_REGNUM, REVERSE_STATE_REGISTER },
  79. { TILEGX_R16_REGNUM, REVERSE_STATE_REGISTER },
  80. { TILEGX_R17_REGNUM, REVERSE_STATE_REGISTER },
  81. { TILEGX_R18_REGNUM, REVERSE_STATE_REGISTER },
  82. { TILEGX_R19_REGNUM, REVERSE_STATE_REGISTER },
  83. { TILEGX_R20_REGNUM, REVERSE_STATE_REGISTER },
  84. { TILEGX_R21_REGNUM, REVERSE_STATE_REGISTER },
  85. { TILEGX_R22_REGNUM, REVERSE_STATE_REGISTER },
  86. { TILEGX_R23_REGNUM, REVERSE_STATE_REGISTER },
  87. { TILEGX_R24_REGNUM, REVERSE_STATE_REGISTER },
  88. { TILEGX_R25_REGNUM, REVERSE_STATE_REGISTER },
  89. { TILEGX_R26_REGNUM, REVERSE_STATE_REGISTER },
  90. { TILEGX_R27_REGNUM, REVERSE_STATE_REGISTER },
  91. { TILEGX_R28_REGNUM, REVERSE_STATE_REGISTER },
  92. { TILEGX_R29_REGNUM, REVERSE_STATE_REGISTER },
  93. { TILEGX_R30_REGNUM, REVERSE_STATE_REGISTER },
  94. { TILEGX_R31_REGNUM, REVERSE_STATE_REGISTER },
  95. { TILEGX_R32_REGNUM, REVERSE_STATE_REGISTER },
  96. { TILEGX_R33_REGNUM, REVERSE_STATE_REGISTER },
  97. { TILEGX_R34_REGNUM, REVERSE_STATE_REGISTER },
  98. { TILEGX_R35_REGNUM, REVERSE_STATE_REGISTER },
  99. { TILEGX_R36_REGNUM, REVERSE_STATE_REGISTER },
  100. { TILEGX_R37_REGNUM, REVERSE_STATE_REGISTER },
  101. { TILEGX_R38_REGNUM, REVERSE_STATE_REGISTER },
  102. { TILEGX_R39_REGNUM, REVERSE_STATE_REGISTER },
  103. { TILEGX_R40_REGNUM, REVERSE_STATE_REGISTER },
  104. { TILEGX_R41_REGNUM, REVERSE_STATE_REGISTER },
  105. { TILEGX_R42_REGNUM, REVERSE_STATE_REGISTER },
  106. { TILEGX_R43_REGNUM, REVERSE_STATE_REGISTER },
  107. { TILEGX_R44_REGNUM, REVERSE_STATE_REGISTER },
  108. { TILEGX_R45_REGNUM, REVERSE_STATE_REGISTER },
  109. { TILEGX_R46_REGNUM, REVERSE_STATE_REGISTER },
  110. { TILEGX_R47_REGNUM, REVERSE_STATE_REGISTER },
  111. { TILEGX_R48_REGNUM, REVERSE_STATE_REGISTER },
  112. { TILEGX_R49_REGNUM, REVERSE_STATE_REGISTER },
  113. { TILEGX_R50_REGNUM, REVERSE_STATE_REGISTER },
  114. { TILEGX_R51_REGNUM, REVERSE_STATE_REGISTER },
  115. { TILEGX_R52_REGNUM, REVERSE_STATE_REGISTER },
  116. { TILEGX_TP_REGNUM, REVERSE_STATE_REGISTER },
  117. { TILEGX_SP_REGNUM, REVERSE_STATE_REGISTER },
  118. { TILEGX_LR_REGNUM, REVERSE_STATE_REGISTER },
  119. { 0, REVERSE_STATE_UNKNOWN },
  120. { 0, REVERSE_STATE_UNKNOWN },
  121. { 0, REVERSE_STATE_UNKNOWN },
  122. { 0, REVERSE_STATE_UNKNOWN },
  123. { 0, REVERSE_STATE_UNKNOWN },
  124. { 0, REVERSE_STATE_UNKNOWN },
  125. { 0, REVERSE_STATE_UNKNOWN },
  126. { TILEGX_ZERO_REGNUM, REVERSE_STATE_VALUE }
  127. };
  128. /* Implement the "register_name" gdbarch method. */
  129. static const char *
  130. tilegx_register_name (struct gdbarch *gdbarch, int regnum)
  131. {
  132. static const char *const register_names[TILEGX_NUM_REGS] =
  133. {
  134. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  135. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  136. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  137. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  138. "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
  139. "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
  140. "r48", "r49", "r50", "r51", "r52", "tp", "sp", "lr",
  141. "sn", "idn0", "idn1", "udn0", "udn1", "udn2", "udn3", "zero",
  142. "pc", "faultnum",
  143. };
  144. if (regnum < 0 || regnum >= TILEGX_NUM_REGS)
  145. internal_error (__FILE__, __LINE__,
  146. "tilegx_register_name: invalid register number %d",
  147. regnum);
  148. return register_names[regnum];
  149. }
  150. /* This is the implementation of gdbarch method register_type. */
  151. static struct type *
  152. tilegx_register_type (struct gdbarch *gdbarch, int regnum)
  153. {
  154. if (regnum == TILEGX_PC_REGNUM)
  155. return builtin_type (gdbarch)->builtin_func_ptr;
  156. else
  157. return builtin_type (gdbarch)->builtin_uint64;
  158. }
  159. /* This is the implementation of gdbarch method dwarf2_reg_to_regnum. */
  160. static int
  161. tilegx_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
  162. {
  163. return num;
  164. }
  165. /* Makes the decision of whether a given type is a scalar type.
  166. Scalar types are returned in the registers r2-r11 as they fit. */
  167. static int
  168. tilegx_type_is_scalar (struct type *t)
  169. {
  170. return (t->code () != TYPE_CODE_STRUCT
  171. && t->code () != TYPE_CODE_UNION
  172. && t->code () != TYPE_CODE_ARRAY);
  173. }
  174. /* Returns non-zero if the given struct type will be returned using
  175. a special convention, rather than the normal function return method.
  176. Used in the context of the "return" command, and target function
  177. calls from the debugger. */
  178. static int
  179. tilegx_use_struct_convention (struct type *type)
  180. {
  181. /* Only scalars which fit in R0 - R9 can be returned in registers.
  182. Otherwise, they are returned via a pointer passed in R0. */
  183. return (!tilegx_type_is_scalar (type)
  184. && (TYPE_LENGTH (type) > (1 + TILEGX_R9_REGNUM - TILEGX_R0_REGNUM)
  185. * tilegx_reg_size));
  186. }
  187. /* Find a function's return value in the appropriate registers (in
  188. REGCACHE), and copy it into VALBUF. */
  189. static void
  190. tilegx_extract_return_value (struct type *type, struct regcache *regcache,
  191. gdb_byte *valbuf)
  192. {
  193. int len = TYPE_LENGTH (type);
  194. int i, regnum = TILEGX_R0_REGNUM;
  195. for (i = 0; i < len; i += tilegx_reg_size)
  196. regcache->raw_read (regnum++, valbuf + i);
  197. }
  198. /* Copy the function return value from VALBUF into the proper
  199. location for a function return.
  200. Called only in the context of the "return" command. */
  201. static void
  202. tilegx_store_return_value (struct type *type, struct regcache *regcache,
  203. const void *valbuf)
  204. {
  205. if (TYPE_LENGTH (type) < tilegx_reg_size)
  206. {
  207. /* Add leading zeros to the (little-endian) value. */
  208. gdb_byte buf[tilegx_reg_size] = { 0 };
  209. memcpy (buf, valbuf, TYPE_LENGTH (type));
  210. regcache->raw_write (TILEGX_R0_REGNUM, buf);
  211. }
  212. else
  213. {
  214. int len = TYPE_LENGTH (type);
  215. int i, regnum = TILEGX_R0_REGNUM;
  216. for (i = 0; i < len; i += tilegx_reg_size)
  217. regcache->raw_write (regnum++, (gdb_byte *) valbuf + i);
  218. }
  219. }
  220. /* This is the implementation of gdbarch method return_value. */
  221. static enum return_value_convention
  222. tilegx_return_value (struct gdbarch *gdbarch, struct value *function,
  223. struct type *type, struct regcache *regcache,
  224. gdb_byte *readbuf, const gdb_byte *writebuf)
  225. {
  226. if (tilegx_use_struct_convention (type))
  227. return RETURN_VALUE_STRUCT_CONVENTION;
  228. if (writebuf)
  229. tilegx_store_return_value (type, regcache, writebuf);
  230. else if (readbuf)
  231. tilegx_extract_return_value (type, regcache, readbuf);
  232. return RETURN_VALUE_REGISTER_CONVENTION;
  233. }
  234. /* This is the implementation of gdbarch method frame_align. */
  235. static CORE_ADDR
  236. tilegx_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  237. {
  238. return addr & -8;
  239. }
  240. /* Implement the "push_dummy_call" gdbarch method. */
  241. static CORE_ADDR
  242. tilegx_push_dummy_call (struct gdbarch *gdbarch,
  243. struct value *function,
  244. struct regcache *regcache,
  245. CORE_ADDR bp_addr, int nargs,
  246. struct value **args,
  247. CORE_ADDR sp, function_call_return_method return_method,
  248. CORE_ADDR struct_addr)
  249. {
  250. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  251. CORE_ADDR stack_dest = sp;
  252. int argreg = TILEGX_R0_REGNUM;
  253. int i, j;
  254. int typelen, slacklen;
  255. static const gdb_byte four_zero_words[16] = { 0 };
  256. /* If struct_return is 1, then the struct return address will
  257. consume one argument-passing register. */
  258. if (return_method == return_method_struct)
  259. regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  260. /* Arguments are passed in R0 - R9, and as soon as an argument
  261. will not fit completely in the remaining registers, then it,
  262. and all remaining arguments, are put on the stack. */
  263. for (i = 0; i < nargs && argreg <= TILEGX_R9_REGNUM; i++)
  264. {
  265. const gdb_byte *val;
  266. typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
  267. if (typelen > (TILEGX_R9_REGNUM - argreg + 1) * tilegx_reg_size)
  268. break;
  269. /* Put argument into registers wordwise. */
  270. val = value_contents (args[i]).data ();
  271. for (j = 0; j < typelen; j += tilegx_reg_size)
  272. {
  273. /* ISSUE: Why special handling for "typelen = 4x + 1"?
  274. I don't ever see "typelen" values except 4 and 8. */
  275. int n = (typelen - j == 1) ? 1 : tilegx_reg_size;
  276. ULONGEST w = extract_unsigned_integer (val + j, n, byte_order);
  277. regcache_cooked_write_unsigned (regcache, argreg++, w);
  278. }
  279. }
  280. /* Align SP. */
  281. stack_dest = tilegx_frame_align (gdbarch, stack_dest);
  282. /* Loop backwards through remaining arguments and push them on
  283. the stack, word aligned. */
  284. for (j = nargs - 1; j >= i; j--)
  285. {
  286. const gdb_byte *contents = value_contents (args[j]).data ();
  287. typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
  288. slacklen = align_up (typelen, 8) - typelen;
  289. gdb::byte_vector val (typelen + slacklen);
  290. memcpy (val.data (), contents, typelen);
  291. memset (val.data () + typelen, 0, slacklen);
  292. /* Now write data to the stack. The stack grows downwards. */
  293. stack_dest -= typelen + slacklen;
  294. write_memory (stack_dest, val.data (), typelen + slacklen);
  295. }
  296. /* Add 16 bytes for linkage space to the stack. */
  297. stack_dest = stack_dest - 16;
  298. write_memory (stack_dest, four_zero_words, 16);
  299. /* Update stack pointer. */
  300. regcache_cooked_write_unsigned (regcache, TILEGX_SP_REGNUM, stack_dest);
  301. /* Set the return address register to point to the entry point of
  302. the program, where a breakpoint lies in wait. */
  303. regcache_cooked_write_unsigned (regcache, TILEGX_LR_REGNUM, bp_addr);
  304. return stack_dest;
  305. }
  306. /* Decode the instructions within the given address range.
  307. Decide when we must have reached the end of the function prologue.
  308. If a frame_info pointer is provided, fill in its saved_regs etc.
  309. Returns the address of the first instruction after the prologue.
  310. NOTE: This is often called with start_addr being the start of some
  311. function, and end_addr being the current PC. */
  312. static CORE_ADDR
  313. tilegx_analyze_prologue (struct gdbarch* gdbarch,
  314. CORE_ADDR start_addr, CORE_ADDR end_addr,
  315. struct tilegx_frame_cache *cache,
  316. struct frame_info *next_frame)
  317. {
  318. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  319. CORE_ADDR next_addr;
  320. CORE_ADDR prolog_end = end_addr;
  321. gdb_byte instbuf[32 * TILEGX_BUNDLE_SIZE_IN_BYTES];
  322. CORE_ADDR instbuf_start;
  323. unsigned int instbuf_size;
  324. int status;
  325. bfd_uint64_t bundle;
  326. struct tilegx_decoded_instruction
  327. decoded[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
  328. int num_insns;
  329. struct tilegx_reverse_regs reverse_frame[TILEGX_NUM_PHYS_REGS];
  330. struct tilegx_reverse_regs
  331. new_reverse_frame[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
  332. int dest_regs[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
  333. int reverse_frame_valid, prolog_done, branch_seen, lr_saved_on_stack_p;
  334. LONGEST prev_sp_value;
  335. int i, j;
  336. if (start_addr >= end_addr
  337. || (start_addr % TILEGX_BUNDLE_ALIGNMENT_IN_BYTES) != 0)
  338. return end_addr;
  339. /* Initialize the reverse frame. This maps the CURRENT frame's
  340. registers to the outer frame's registers (the frame on the
  341. stack goes the other way). */
  342. memcpy (&reverse_frame, &template_reverse_regs, sizeof (reverse_frame));
  343. prolog_done = 0;
  344. branch_seen = 0;
  345. prev_sp_value = 0;
  346. lr_saved_on_stack_p = 0;
  347. /* To cut down on round-trip overhead, we fetch multiple bundles
  348. at once. These variables describe the range of memory we have
  349. prefetched. */
  350. instbuf_start = 0;
  351. instbuf_size = 0;
  352. for (next_addr = start_addr;
  353. next_addr < end_addr;
  354. next_addr += TILEGX_BUNDLE_SIZE_IN_BYTES)
  355. {
  356. /* Retrieve the next instruction. */
  357. if (next_addr - instbuf_start >= instbuf_size)
  358. {
  359. /* Figure out how many bytes to fetch. Don't span a page
  360. boundary since that might cause an unnecessary memory
  361. error. */
  362. unsigned int size_on_same_page = 4096 - (next_addr & 4095);
  363. instbuf_size = sizeof instbuf;
  364. if (instbuf_size > size_on_same_page)
  365. instbuf_size = size_on_same_page;
  366. instbuf_size = std::min ((CORE_ADDR) instbuf_size,
  367. (end_addr - next_addr));
  368. instbuf_start = next_addr;
  369. status = safe_frame_unwind_memory (next_frame, instbuf_start,
  370. {instbuf, instbuf_size});
  371. if (status == 0)
  372. memory_error (TARGET_XFER_E_IO, next_addr);
  373. }
  374. reverse_frame_valid = 0;
  375. bundle = extract_unsigned_integer (&instbuf[next_addr - instbuf_start],
  376. 8, byte_order);
  377. num_insns = parse_insn_tilegx (bundle, next_addr, decoded);
  378. for (i = 0; i < num_insns; i++)
  379. {
  380. struct tilegx_decoded_instruction *this_insn = &decoded[i];
  381. long long *operands = this_insn->operand_values;
  382. const struct tilegx_opcode *opcode = this_insn->opcode;
  383. switch (opcode->mnemonic)
  384. {
  385. case TILEGX_OPC_ST:
  386. if (cache
  387. && reverse_frame[operands[0]].state == REVERSE_STATE_VALUE
  388. && reverse_frame[operands[1]].state
  389. == REVERSE_STATE_REGISTER)
  390. {
  391. LONGEST saved_address = reverse_frame[operands[0]].value;
  392. unsigned saved_register
  393. = (unsigned) reverse_frame[operands[1]].value;
  394. cache->saved_regs[saved_register].set_addr (saved_address);
  395. }
  396. else if (cache
  397. && (operands[0] == TILEGX_SP_REGNUM)
  398. && (operands[1] == TILEGX_LR_REGNUM))
  399. lr_saved_on_stack_p = 1;
  400. break;
  401. case TILEGX_OPC_ADDI:
  402. case TILEGX_OPC_ADDLI:
  403. if (cache
  404. && operands[0] == TILEGX_SP_REGNUM
  405. && operands[1] == TILEGX_SP_REGNUM
  406. && reverse_frame[operands[1]].state == REVERSE_STATE_REGISTER)
  407. {
  408. /* Special case. We're fixing up the stack frame. */
  409. uint64_t hopefully_sp
  410. = (unsigned) reverse_frame[operands[1]].value;
  411. short op2_as_short = (short) operands[2];
  412. signed char op2_as_char = (signed char) operands[2];
  413. /* Fix up the sign-extension. */
  414. if (opcode->mnemonic == TILEGX_OPC_ADDI)
  415. op2_as_short = op2_as_char;
  416. prev_sp_value = (cache->saved_regs[hopefully_sp].addr ()
  417. - op2_as_short);
  418. new_reverse_frame[i].state = REVERSE_STATE_VALUE;
  419. new_reverse_frame[i].value
  420. = cache->saved_regs[hopefully_sp].addr ();
  421. cache->saved_regs[hopefully_sp].set_value (prev_sp_value);
  422. }
  423. else
  424. {
  425. short op2_as_short = (short) operands[2];
  426. signed char op2_as_char = (signed char) operands[2];
  427. /* Fix up the sign-extension. */
  428. if (opcode->mnemonic == TILEGX_OPC_ADDI)
  429. op2_as_short = op2_as_char;
  430. new_reverse_frame[i] = reverse_frame[operands[1]];
  431. if (new_reverse_frame[i].state == REVERSE_STATE_VALUE)
  432. new_reverse_frame[i].value += op2_as_short;
  433. else
  434. new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  435. }
  436. reverse_frame_valid |= 1 << i;
  437. dest_regs[i] = operands[0];
  438. break;
  439. case TILEGX_OPC_ADD:
  440. if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
  441. && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
  442. {
  443. /* We have values -- we can do this. */
  444. new_reverse_frame[i] = reverse_frame[operands[2]];
  445. new_reverse_frame[i].value
  446. += reverse_frame[operands[i]].value;
  447. }
  448. else
  449. {
  450. /* We don't know anything about the values. Punt. */
  451. new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  452. }
  453. reverse_frame_valid |= 1 << i;
  454. dest_regs[i] = operands[0];
  455. break;
  456. case TILEGX_OPC_MOVE:
  457. new_reverse_frame[i] = reverse_frame[operands[1]];
  458. reverse_frame_valid |= 1 << i;
  459. dest_regs[i] = operands[0];
  460. break;
  461. case TILEGX_OPC_MOVEI:
  462. case TILEGX_OPC_MOVELI:
  463. new_reverse_frame[i].state = REVERSE_STATE_VALUE;
  464. new_reverse_frame[i].value = operands[1];
  465. reverse_frame_valid |= 1 << i;
  466. dest_regs[i] = operands[0];
  467. break;
  468. case TILEGX_OPC_ORI:
  469. if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE)
  470. {
  471. /* We have a value in A -- we can do this. */
  472. new_reverse_frame[i] = reverse_frame[operands[1]];
  473. new_reverse_frame[i].value
  474. = reverse_frame[operands[1]].value | operands[2];
  475. }
  476. else if (operands[2] == 0)
  477. {
  478. /* This is a move. */
  479. new_reverse_frame[i] = reverse_frame[operands[1]];
  480. }
  481. else
  482. {
  483. /* We don't know anything about the values. Punt. */
  484. new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  485. }
  486. reverse_frame_valid |= 1 << i;
  487. dest_regs[i] = operands[0];
  488. break;
  489. case TILEGX_OPC_OR:
  490. if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
  491. && reverse_frame[operands[1]].value == 0)
  492. {
  493. /* This is a move. */
  494. new_reverse_frame[i] = reverse_frame[operands[2]];
  495. }
  496. else if (reverse_frame[operands[2]].state == REVERSE_STATE_VALUE
  497. && reverse_frame[operands[2]].value == 0)
  498. {
  499. /* This is a move. */
  500. new_reverse_frame[i] = reverse_frame[operands[1]];
  501. }
  502. else
  503. {
  504. /* We don't know anything about the values. Punt. */
  505. new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  506. }
  507. reverse_frame_valid |= 1 << i;
  508. dest_regs[i] = operands[0];
  509. break;
  510. case TILEGX_OPC_SUB:
  511. if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
  512. && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
  513. {
  514. /* We have values -- we can do this. */
  515. new_reverse_frame[i] = reverse_frame[operands[1]];
  516. new_reverse_frame[i].value
  517. -= reverse_frame[operands[2]].value;
  518. }
  519. else
  520. {
  521. /* We don't know anything about the values. Punt. */
  522. new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  523. }
  524. reverse_frame_valid |= 1 << i;
  525. dest_regs[i] = operands[0];
  526. break;
  527. case TILEGX_OPC_FNOP:
  528. case TILEGX_OPC_INFO:
  529. case TILEGX_OPC_INFOL:
  530. /* Nothing to see here, move on.
  531. Note that real NOP is treated as a 'real' instruction
  532. because someone must have intended that it be there.
  533. It therefore terminates the prolog. */
  534. break;
  535. case TILEGX_OPC_J:
  536. case TILEGX_OPC_JAL:
  537. case TILEGX_OPC_BEQZ:
  538. case TILEGX_OPC_BEQZT:
  539. case TILEGX_OPC_BGEZ:
  540. case TILEGX_OPC_BGEZT:
  541. case TILEGX_OPC_BGTZ:
  542. case TILEGX_OPC_BGTZT:
  543. case TILEGX_OPC_BLBC:
  544. case TILEGX_OPC_BLBCT:
  545. case TILEGX_OPC_BLBS:
  546. case TILEGX_OPC_BLBST:
  547. case TILEGX_OPC_BLEZ:
  548. case TILEGX_OPC_BLEZT:
  549. case TILEGX_OPC_BLTZ:
  550. case TILEGX_OPC_BLTZT:
  551. case TILEGX_OPC_BNEZ:
  552. case TILEGX_OPC_BNEZT:
  553. case TILEGX_OPC_IRET:
  554. case TILEGX_OPC_JALR:
  555. case TILEGX_OPC_JALRP:
  556. case TILEGX_OPC_JR:
  557. case TILEGX_OPC_JRP:
  558. case TILEGX_OPC_SWINT0:
  559. case TILEGX_OPC_SWINT1:
  560. case TILEGX_OPC_SWINT2:
  561. case TILEGX_OPC_SWINT3:
  562. /* We're really done -- this is a branch. */
  563. branch_seen = 1;
  564. prolog_done = 1;
  565. break;
  566. default:
  567. /* We don't know or care what this instruction is.
  568. All we know is that it isn't part of a prolog, and if
  569. there's a destination register, we're trashing it. */
  570. prolog_done = 1;
  571. for (j = 0; j < opcode->num_operands; j++)
  572. {
  573. if (this_insn->operands[j]->is_dest_reg)
  574. {
  575. dest_regs[i] = operands[j];
  576. new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
  577. reverse_frame_valid |= 1 << i;
  578. break;
  579. }
  580. }
  581. break;
  582. }
  583. }
  584. /* Now update the reverse frames. */
  585. for (i = 0; i < num_insns; i++)
  586. {
  587. /* ISSUE: Does this properly handle "network" registers? */
  588. if ((reverse_frame_valid & (1 << i))
  589. && dest_regs[i] != TILEGX_ZERO_REGNUM)
  590. reverse_frame[dest_regs[i]] = new_reverse_frame[i];
  591. }
  592. if (prev_sp_value != 0)
  593. {
  594. /* GCC uses R52 as a frame pointer. Have we seen "move r52, sp"? */
  595. if (reverse_frame[TILEGX_R52_REGNUM].state == REVERSE_STATE_REGISTER
  596. && reverse_frame[TILEGX_R52_REGNUM].value == TILEGX_SP_REGNUM)
  597. {
  598. reverse_frame[TILEGX_R52_REGNUM].state = REVERSE_STATE_VALUE;
  599. reverse_frame[TILEGX_R52_REGNUM].value = prev_sp_value;
  600. }
  601. prev_sp_value = 0;
  602. }
  603. if (prolog_done && prolog_end == end_addr)
  604. {
  605. /* We found non-prolog code. As such, _this_ instruction
  606. is the one after the prolog. We keep processing, because
  607. there may be more prolog code in there, but this is what
  608. we'll return. */
  609. /* ISSUE: There may not have actually been a prologue, and
  610. we may have simply skipped some random instructions. */
  611. prolog_end = next_addr;
  612. }
  613. if (branch_seen)
  614. {
  615. /* We saw a branch. The prolog absolutely must be over. */
  616. break;
  617. }
  618. }
  619. if (prolog_end == end_addr && cache)
  620. {
  621. /* We may have terminated the prolog early, and we're certainly
  622. at THIS point right now. It's possible that the values of
  623. registers we need are currently actually in other registers
  624. (and haven't been written to memory yet). Go find them. */
  625. for (i = 0; i < TILEGX_NUM_PHYS_REGS; i++)
  626. {
  627. if (reverse_frame[i].state == REVERSE_STATE_REGISTER
  628. && reverse_frame[i].value != i)
  629. {
  630. unsigned saved_register = (unsigned) reverse_frame[i].value;
  631. cache->saved_regs[saved_register].set_realreg (i);
  632. }
  633. }
  634. }
  635. if (lr_saved_on_stack_p)
  636. {
  637. CORE_ADDR addr = cache->saved_regs[TILEGX_SP_REGNUM].addr ();
  638. cache->saved_regs[TILEGX_LR_REGNUM].set_addr (addr);
  639. }
  640. return prolog_end;
  641. }
  642. /* This is the implementation of gdbarch method skip_prologue. */
  643. static CORE_ADDR
  644. tilegx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  645. {
  646. CORE_ADDR func_start, end_pc;
  647. struct obj_section *s;
  648. /* This is the preferred method, find the end of the prologue by
  649. using the debugging information. */
  650. if (find_pc_partial_function (start_pc, NULL, &func_start, NULL))
  651. {
  652. CORE_ADDR post_prologue_pc
  653. = skip_prologue_using_sal (gdbarch, func_start);
  654. if (post_prologue_pc != 0)
  655. return std::max (start_pc, post_prologue_pc);
  656. }
  657. /* Don't straddle a section boundary. */
  658. s = find_pc_section (start_pc);
  659. end_pc = start_pc + 8 * TILEGX_BUNDLE_SIZE_IN_BYTES;
  660. if (s != NULL)
  661. end_pc = std::min (end_pc, s->endaddr ());
  662. /* Otherwise, try to skip prologue the hard way. */
  663. return tilegx_analyze_prologue (gdbarch,
  664. start_pc,
  665. end_pc,
  666. NULL, NULL);
  667. }
  668. /* This is the implementation of gdbarch method stack_frame_destroyed_p. */
  669. static int
  670. tilegx_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  671. {
  672. CORE_ADDR func_addr = 0, func_end = 0;
  673. if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  674. {
  675. CORE_ADDR addr = func_end - TILEGX_BUNDLE_SIZE_IN_BYTES;
  676. /* FIXME: Find the actual epilogue. */
  677. /* HACK: Just assume the final bundle is the "ret" instruction". */
  678. if (pc > addr)
  679. return 1;
  680. }
  681. return 0;
  682. }
  683. /* This is the implementation of gdbarch method get_longjmp_target. */
  684. static int
  685. tilegx_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  686. {
  687. struct gdbarch *gdbarch = get_frame_arch (frame);
  688. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  689. CORE_ADDR jb_addr;
  690. gdb_byte buf[8];
  691. jb_addr = get_frame_register_unsigned (frame, TILEGX_R0_REGNUM);
  692. /* TileGX jmp_buf contains 32 elements of type __uint_reg_t which
  693. has a size of 8 bytes. The return address is stored in the 25th
  694. slot. */
  695. if (target_read_memory (jb_addr + 25 * 8, buf, 8))
  696. return 0;
  697. *pc = extract_unsigned_integer (buf, 8, byte_order);
  698. return 1;
  699. }
  700. /* by assigning the 'faultnum' reg in kernel pt_regs with this value,
  701. kernel do_signal will not check r0. see tilegx kernel/signal.c
  702. for details. */
  703. #define INT_SWINT_1_SIGRETURN (~0)
  704. /* Implement the "write_pc" gdbarch method. */
  705. static void
  706. tilegx_write_pc (struct regcache *regcache, CORE_ADDR pc)
  707. {
  708. regcache_cooked_write_unsigned (regcache, TILEGX_PC_REGNUM, pc);
  709. /* We must be careful with modifying the program counter. If we
  710. just interrupted a system call, the kernel might try to restart
  711. it when we resume the inferior. On restarting the system call,
  712. the kernel will try backing up the program counter even though it
  713. no longer points at the system call. This typically results in a
  714. SIGSEGV or SIGILL. We can prevent this by writing INT_SWINT_1_SIGRETURN
  715. in the "faultnum" pseudo-register.
  716. Note that "faultnum" is saved when setting up a dummy call frame.
  717. This means that it is properly restored when that frame is
  718. popped, and that the interrupted system call will be restarted
  719. when we resume the inferior on return from a function call from
  720. within GDB. In all other cases the system call will not be
  721. restarted. */
  722. regcache_cooked_write_unsigned (regcache, TILEGX_FAULTNUM_REGNUM,
  723. INT_SWINT_1_SIGRETURN);
  724. }
  725. /* 64-bit pattern for a { bpt ; nop } bundle. */
  726. constexpr gdb_byte tilegx_break_insn[] =
  727. { 0x00, 0x50, 0x48, 0x51, 0xae, 0x44, 0x6a, 0x28 };
  728. typedef BP_MANIPULATION (tilegx_break_insn) tilegx_breakpoint;
  729. /* Normal frames. */
  730. static struct tilegx_frame_cache *
  731. tilegx_frame_cache (struct frame_info *this_frame, void **this_cache)
  732. {
  733. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  734. struct tilegx_frame_cache *cache;
  735. CORE_ADDR current_pc;
  736. if (*this_cache)
  737. return (struct tilegx_frame_cache *) *this_cache;
  738. cache = FRAME_OBSTACK_ZALLOC (struct tilegx_frame_cache);
  739. *this_cache = cache;
  740. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  741. cache->base = 0;
  742. cache->start_pc = get_frame_func (this_frame);
  743. current_pc = get_frame_pc (this_frame);
  744. cache->base = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
  745. cache->saved_regs[TILEGX_SP_REGNUM].set_value (cache->base);
  746. if (cache->start_pc)
  747. tilegx_analyze_prologue (gdbarch, cache->start_pc, current_pc,
  748. cache, this_frame);
  749. cache->saved_regs[TILEGX_PC_REGNUM] = cache->saved_regs[TILEGX_LR_REGNUM];
  750. return cache;
  751. }
  752. /* Retrieve the value of REGNUM in FRAME. */
  753. static struct value*
  754. tilegx_frame_prev_register (struct frame_info *this_frame,
  755. void **this_cache,
  756. int regnum)
  757. {
  758. struct tilegx_frame_cache *info =
  759. tilegx_frame_cache (this_frame, this_cache);
  760. return trad_frame_get_prev_register (this_frame, info->saved_regs,
  761. regnum);
  762. }
  763. /* Build frame id. */
  764. static void
  765. tilegx_frame_this_id (struct frame_info *this_frame, void **this_cache,
  766. struct frame_id *this_id)
  767. {
  768. struct tilegx_frame_cache *info =
  769. tilegx_frame_cache (this_frame, this_cache);
  770. /* This marks the outermost frame. */
  771. if (info->base == 0)
  772. return;
  773. (*this_id) = frame_id_build (info->base, info->start_pc);
  774. }
  775. static CORE_ADDR
  776. tilegx_frame_base_address (struct frame_info *this_frame, void **this_cache)
  777. {
  778. struct tilegx_frame_cache *cache =
  779. tilegx_frame_cache (this_frame, this_cache);
  780. return cache->base;
  781. }
  782. static const struct frame_unwind tilegx_frame_unwind = {
  783. "tilegx prologue",
  784. NORMAL_FRAME,
  785. default_frame_unwind_stop_reason,
  786. tilegx_frame_this_id,
  787. tilegx_frame_prev_register,
  788. NULL, /* const struct frame_data *unwind_data */
  789. default_frame_sniffer, /* frame_sniffer_ftype *sniffer */
  790. NULL /* frame_prev_pc_ftype *prev_pc */
  791. };
  792. static const struct frame_base tilegx_frame_base = {
  793. &tilegx_frame_unwind,
  794. tilegx_frame_base_address,
  795. tilegx_frame_base_address,
  796. tilegx_frame_base_address
  797. };
  798. /* We cannot read/write the "special" registers. */
  799. static int
  800. tilegx_cannot_reference_register (struct gdbarch *gdbarch, int regno)
  801. {
  802. if (regno >= 0 && regno < TILEGX_NUM_EASY_REGS)
  803. return 0;
  804. else if (regno == TILEGX_PC_REGNUM
  805. || regno == TILEGX_FAULTNUM_REGNUM)
  806. return 0;
  807. else
  808. return 1;
  809. }
  810. static struct gdbarch *
  811. tilegx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  812. {
  813. struct gdbarch *gdbarch;
  814. int arch_size = 64;
  815. /* Handle arch_size == 32 or 64. Default to 64. */
  816. if (info.abfd)
  817. arch_size = bfd_get_arch_size (info.abfd);
  818. /* Try to find a pre-existing architecture. */
  819. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  820. arches != NULL;
  821. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  822. {
  823. /* We only have two flavors -- just make sure arch_size matches. */
  824. if (gdbarch_ptr_bit (arches->gdbarch) == arch_size)
  825. return (arches->gdbarch);
  826. }
  827. gdbarch = gdbarch_alloc (&info, NULL);
  828. /* Basic register fields and methods, datatype sizes and stuff. */
  829. /* There are 64 physical registers which can be referenced by
  830. instructions (although only 56 of them can actually be
  831. debugged) and 1 magic register (the PC). The other three
  832. magic registers (ex1, syscall, orig_r0) which are known to
  833. "ptrace" are ignored by "gdb". Note that we simply pretend
  834. that there are 65 registers, and no "pseudo registers". */
  835. set_gdbarch_num_regs (gdbarch, TILEGX_NUM_REGS);
  836. set_gdbarch_num_pseudo_regs (gdbarch, 0);
  837. set_gdbarch_sp_regnum (gdbarch, TILEGX_SP_REGNUM);
  838. set_gdbarch_pc_regnum (gdbarch, TILEGX_PC_REGNUM);
  839. set_gdbarch_register_name (gdbarch, tilegx_register_name);
  840. set_gdbarch_register_type (gdbarch, tilegx_register_type);
  841. set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  842. set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  843. set_gdbarch_long_bit (gdbarch, arch_size);
  844. set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  845. set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  846. set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  847. set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  848. set_gdbarch_ptr_bit (gdbarch, arch_size);
  849. set_gdbarch_addr_bit (gdbarch, arch_size);
  850. set_gdbarch_cannot_fetch_register (gdbarch,
  851. tilegx_cannot_reference_register);
  852. set_gdbarch_cannot_store_register (gdbarch,
  853. tilegx_cannot_reference_register);
  854. /* Stack grows down. */
  855. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  856. /* Frame Info. */
  857. set_gdbarch_frame_align (gdbarch, tilegx_frame_align);
  858. frame_base_set_default (gdbarch, &tilegx_frame_base);
  859. set_gdbarch_skip_prologue (gdbarch, tilegx_skip_prologue);
  860. set_gdbarch_stack_frame_destroyed_p (gdbarch, tilegx_stack_frame_destroyed_p);
  861. /* Map debug registers into internal register numbers. */
  862. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, tilegx_dwarf2_reg_to_regnum);
  863. /* These values and methods are used when gdb calls a target function. */
  864. set_gdbarch_push_dummy_call (gdbarch, tilegx_push_dummy_call);
  865. set_gdbarch_get_longjmp_target (gdbarch, tilegx_get_longjmp_target);
  866. set_gdbarch_write_pc (gdbarch, tilegx_write_pc);
  867. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  868. tilegx_breakpoint::kind_from_pc);
  869. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  870. tilegx_breakpoint::bp_from_kind);
  871. set_gdbarch_return_value (gdbarch, tilegx_return_value);
  872. gdbarch_init_osabi (info, gdbarch);
  873. dwarf2_append_unwinders (gdbarch);
  874. frame_unwind_append_unwinder (gdbarch, &tilegx_frame_unwind);
  875. return gdbarch;
  876. }
  877. void _initialize_tilegx_tdep ();
  878. void
  879. _initialize_tilegx_tdep ()
  880. {
  881. register_gdbarch_init (bfd_arch_tilegx, tilegx_gdbarch_init);
  882. }