GDB (xrefs)
/tmp/gdb-8.1/gdb/tilegx-tdep.c
Go to the documentation of this file.
1 /* Target-dependent code for the Tilera TILE-Gx processor.
2 
3  Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "defs.h"
21 #include "frame.h"
22 #include "frame-base.h"
23 #include "frame-unwind.h"
24 #include "dwarf2-frame.h"
25 #include "trad-frame.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "gdbcmd.h"
29 #include "gdbcore.h"
30 #include "value.h"
31 #include "dis-asm.h"
32 #include "inferior.h"
33 #include "arch-utils.h"
34 #include "regcache.h"
35 #include "regset.h"
36 #include "osabi.h"
37 #include "linux-tdep.h"
38 #include "objfiles.h"
39 #include "solib-svr4.h"
40 #include "tilegx-tdep.h"
41 #include "opcode/tilegx.h"
42 #include <algorithm>
43 #include "common/byte-vector.h"
44 
46 {
47  /* Base address. */
49  /* Function start. */
51 
52  /* Table of saved registers. */
54 };
55 
56 /* Register state values used by analyze_prologue. */
58  {
62  };
63 
64 /* Register state used by analyze_prologue(). */
66 {
69 };
70 
71 static const struct tilegx_reverse_regs
73  {
130  { 0, REVERSE_STATE_UNKNOWN },
131  { 0, REVERSE_STATE_UNKNOWN },
132  { 0, REVERSE_STATE_UNKNOWN },
133  { 0, REVERSE_STATE_UNKNOWN },
134  { 0, REVERSE_STATE_UNKNOWN },
135  { 0, REVERSE_STATE_UNKNOWN },
136  { 0, REVERSE_STATE_UNKNOWN },
138  };
139 
140 /* Implement the "register_name" gdbarch method. */
141 
142 static const char *
144 {
145  static const char *const register_names[TILEGX_NUM_REGS] =
146  {
147  "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
148  "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
149  "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
150  "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
151  "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
152  "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
153  "r48", "r49", "r50", "r51", "r52", "tp", "sp", "lr",
154  "sn", "idn0", "idn1", "udn0", "udn1", "udn2", "udn3", "zero",
155  "pc", "faultnum",
156  };
157 
158  if (regnum < 0 || regnum >= TILEGX_NUM_REGS)
159  internal_error (__FILE__, __LINE__,
160  "tilegx_register_name: invalid register number %d",
161  regnum);
162 
163  return register_names[regnum];
164 }
165 
166 /* This is the implementation of gdbarch method register_type. */
167 
168 static struct type *
170 {
171  if (regnum == TILEGX_PC_REGNUM)
173  else
175 }
176 
177 /* This is the implementation of gdbarch method dwarf2_reg_to_regnum. */
178 
179 static int
181 {
182  return num;
183 }
184 
185 /* Makes the decision of whether a given type is a scalar type.
186  Scalar types are returned in the registers r2-r11 as they fit. */
187 
188 static int
190 {
191  return (TYPE_CODE(t) != TYPE_CODE_STRUCT
192  && TYPE_CODE(t) != TYPE_CODE_UNION
193  && TYPE_CODE(t) != TYPE_CODE_ARRAY);
194 }
195 
196 /* Returns non-zero if the given struct type will be returned using
197  a special convention, rather than the normal function return method.
198  Used in the context of the "return" command, and target function
199  calls from the debugger. */
200 
201 static int
203 {
204  /* Only scalars which fit in R0 - R9 can be returned in registers.
205  Otherwise, they are returned via a pointer passed in R0. */
206  return (!tilegx_type_is_scalar (type)
208  * tilegx_reg_size));
209 }
210 
211 /* Find a function's return value in the appropriate registers (in
212  REGCACHE), and copy it into VALBUF. */
213 
214 static void
216  gdb_byte *valbuf)
217 {
218  int len = TYPE_LENGTH (type);
219  int i, regnum = TILEGX_R0_REGNUM;
220 
221  for (i = 0; i < len; i += tilegx_reg_size)
222  regcache_raw_read (regcache, regnum++, valbuf + i);
223 }
224 
225 /* Copy the function return value from VALBUF into the proper
226  location for a function return.
227  Called only in the context of the "return" command. */
228 
229 static void
231  const void *valbuf)
232 {
234  {
235  /* Add leading zeros to the (little-endian) value. */
236  gdb_byte buf[tilegx_reg_size] = { 0 };
237 
238  memcpy (buf, valbuf, TYPE_LENGTH (type));
240  }
241  else
242  {
243  int len = TYPE_LENGTH (type);
244  int i, regnum = TILEGX_R0_REGNUM;
245 
246  for (i = 0; i < len; i += tilegx_reg_size)
247  regcache_raw_write (regcache, regnum++, (gdb_byte *) valbuf + i);
248  }
249 }
250 
251 /* This is the implementation of gdbarch method return_value. */
252 
253 static enum return_value_convention
254 tilegx_return_value (struct gdbarch *gdbarch, struct value *function,
255  struct type *type, struct regcache *regcache,
256  gdb_byte *readbuf, const gdb_byte *writebuf)
257 {
260  if (writebuf)
262  else if (readbuf)
265 }
266 
267 /* This is the implementation of gdbarch method frame_align. */
268 
269 static CORE_ADDR
271 {
272  return addr & -8;
273 }
274 
275 
276 /* Implement the "push_dummy_call" gdbarch method. */
277 
278 static CORE_ADDR
280  struct value *function,
281  struct regcache *regcache,
282  CORE_ADDR bp_addr, int nargs,
283  struct value **args,
284  CORE_ADDR sp, int struct_return,
285  CORE_ADDR struct_addr)
286 {
287  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
288  CORE_ADDR stack_dest = sp;
289  int argreg = TILEGX_R0_REGNUM;
290  int i, j;
291  int typelen, slacklen;
292  static const gdb_byte four_zero_words[16] = { 0 };
293 
294  /* If struct_return is 1, then the struct return address will
295  consume one argument-passing register. */
296  if (struct_return)
297  regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
298 
299  /* Arguments are passed in R0 - R9, and as soon as an argument
300  will not fit completely in the remaining registers, then it,
301  and all remaining arguments, are put on the stack. */
302  for (i = 0; i < nargs && argreg <= TILEGX_R9_REGNUM; i++)
303  {
304  const gdb_byte *val;
305  typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
306 
307  if (typelen > (TILEGX_R9_REGNUM - argreg + 1) * tilegx_reg_size)
308  break;
309 
310  /* Put argument into registers wordwise. */
311  val = value_contents (args[i]);
312  for (j = 0; j < typelen; j += tilegx_reg_size)
313  {
314  /* ISSUE: Why special handling for "typelen = 4x + 1"?
315  I don't ever see "typelen" values except 4 and 8. */
316  int n = (typelen - j == 1) ? 1 : tilegx_reg_size;
317  ULONGEST w = extract_unsigned_integer (val + j, n, byte_order);
318 
320  }
321  }
322 
323  /* Align SP. */
324  stack_dest = tilegx_frame_align (gdbarch, stack_dest);
325 
326  /* Loop backwards through remaining arguments and push them on
327  the stack, word aligned. */
328  for (j = nargs - 1; j >= i; j--)
329  {
330  const gdb_byte *contents = value_contents (args[j]);
331 
332  typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
333  slacklen = align_up (typelen, 8) - typelen;
334  gdb::byte_vector val (typelen + slacklen);
335  memcpy (val.data (), contents, typelen);
336  memset (val.data () + typelen, 0, slacklen);
337 
338  /* Now write data to the stack. The stack grows downwards. */
339  stack_dest -= typelen + slacklen;
340  write_memory (stack_dest, val.data (), typelen + slacklen);
341  }
342 
343  /* Add 16 bytes for linkage space to the stack. */
344  stack_dest = stack_dest - 16;
345  write_memory (stack_dest, four_zero_words, 16);
346 
347  /* Update stack pointer. */
349 
350  /* Set the return address register to point to the entry point of
351  the program, where a breakpoint lies in wait. */
353 
354  return stack_dest;
355 }
356 
357 
358 /* Decode the instructions within the given address range.
359  Decide when we must have reached the end of the function prologue.
360  If a frame_info pointer is provided, fill in its saved_regs etc.
361  Returns the address of the first instruction after the prologue.
362  NOTE: This is often called with start_addr being the start of some
363  function, and end_addr being the current PC. */
364 
365 static CORE_ADDR
367  CORE_ADDR start_addr, CORE_ADDR end_addr,
368  struct tilegx_frame_cache *cache,
369  struct frame_info *next_frame)
370 {
371  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
372  CORE_ADDR next_addr;
373  CORE_ADDR prolog_end = end_addr;
374  gdb_byte instbuf[32 * TILEGX_BUNDLE_SIZE_IN_BYTES];
375  CORE_ADDR instbuf_start;
376  unsigned int instbuf_size;
377  int status;
378  bfd_uint64_t bundle;
379  struct tilegx_decoded_instruction
380  decoded[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
381  int num_insns;
382  struct tilegx_reverse_regs reverse_frame[TILEGX_NUM_PHYS_REGS];
383  struct tilegx_reverse_regs
384  new_reverse_frame[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
385  int dest_regs[TILEGX_MAX_INSTRUCTIONS_PER_BUNDLE];
386  int reverse_frame_valid, prolog_done, branch_seen, lr_saved_on_stack_p;
387  LONGEST prev_sp_value;
388  int i, j;
389 
390  if (start_addr >= end_addr
391  || (start_addr % TILEGX_BUNDLE_ALIGNMENT_IN_BYTES) != 0)
392  return end_addr;
393 
394  /* Initialize the reverse frame. This maps the CURRENT frame's
395  registers to the outer frame's registers (the frame on the
396  stack goes the other way). */
397  memcpy (&reverse_frame, &template_reverse_regs, sizeof (reverse_frame));
398 
399  prolog_done = 0;
400  branch_seen = 0;
401  prev_sp_value = 0;
402  lr_saved_on_stack_p = 0;
403 
404  /* To cut down on round-trip overhead, we fetch multiple bundles
405  at once. These variables describe the range of memory we have
406  prefetched. */
407  instbuf_start = 0;
408  instbuf_size = 0;
409 
410  for (next_addr = start_addr;
411  next_addr < end_addr;
412  next_addr += TILEGX_BUNDLE_SIZE_IN_BYTES)
413  {
414  /* Retrieve the next instruction. */
415  if (next_addr - instbuf_start >= instbuf_size)
416  {
417  /* Figure out how many bytes to fetch. Don't span a page
418  boundary since that might cause an unnecessary memory
419  error. */
420  unsigned int size_on_same_page = 4096 - (next_addr & 4095);
421 
422  instbuf_size = sizeof instbuf;
423 
424  if (instbuf_size > size_on_same_page)
425  instbuf_size = size_on_same_page;
426 
427  instbuf_size = std::min ((CORE_ADDR) instbuf_size,
428  (end_addr - next_addr));
429  instbuf_start = next_addr;
430 
431  status = safe_frame_unwind_memory (next_frame, instbuf_start,
432  instbuf, instbuf_size);
433  if (status == 0)
434  memory_error (TARGET_XFER_E_IO, next_addr);
435  }
436 
437  reverse_frame_valid = 0;
438 
439  bundle = extract_unsigned_integer (&instbuf[next_addr - instbuf_start],
440  8, byte_order);
441 
442  num_insns = parse_insn_tilegx (bundle, next_addr, decoded);
443 
444  for (i = 0; i < num_insns; i++)
445  {
446  struct tilegx_decoded_instruction *this_insn = &decoded[i];
447  long long *operands = this_insn->operand_values;
448  const struct tilegx_opcode *opcode = this_insn->opcode;
449 
450  switch (opcode->mnemonic)
451  {
452  case TILEGX_OPC_ST:
453  if (cache
454  && reverse_frame[operands[0]].state == REVERSE_STATE_VALUE
455  && reverse_frame[operands[1]].state
457  {
458  LONGEST saved_address = reverse_frame[operands[0]].value;
459  unsigned saved_register
460  = (unsigned) reverse_frame[operands[1]].value;
461 
462  /* realreg >= 0 and addr != -1 indicates that the
463  value of saved_register is in memory location
464  saved_address. The value of realreg is not
465  meaningful in this case but it must be >= 0.
466  See trad-frame.h. */
467  cache->saved_regs[saved_register].realreg = saved_register;
468  cache->saved_regs[saved_register].addr = saved_address;
469  }
470  else if (cache
471  && (operands[0] == TILEGX_SP_REGNUM)
472  && (operands[1] == TILEGX_LR_REGNUM))
473  lr_saved_on_stack_p = 1;
474  break;
475  case TILEGX_OPC_ADDI:
476  case TILEGX_OPC_ADDLI:
477  if (cache
478  && operands[0] == TILEGX_SP_REGNUM
479  && operands[1] == TILEGX_SP_REGNUM
480  && reverse_frame[operands[1]].state == REVERSE_STATE_REGISTER)
481  {
482  /* Special case. We're fixing up the stack frame. */
483  uint64_t hopefully_sp
484  = (unsigned) reverse_frame[operands[1]].value;
485  short op2_as_short = (short) operands[2];
486  signed char op2_as_char = (signed char) operands[2];
487 
488  /* Fix up the sign-extension. */
489  if (opcode->mnemonic == TILEGX_OPC_ADDI)
490  op2_as_short = op2_as_char;
491  prev_sp_value = (cache->saved_regs[hopefully_sp].addr
492  - op2_as_short);
493 
494  new_reverse_frame[i].state = REVERSE_STATE_VALUE;
495  new_reverse_frame[i].value
496  = cache->saved_regs[hopefully_sp].addr;
498  hopefully_sp, prev_sp_value);
499  }
500  else
501  {
502  short op2_as_short = (short) operands[2];
503  signed char op2_as_char = (signed char) operands[2];
504 
505  /* Fix up the sign-extension. */
506  if (opcode->mnemonic == TILEGX_OPC_ADDI)
507  op2_as_short = op2_as_char;
508 
509  new_reverse_frame[i] = reverse_frame[operands[1]];
510  if (new_reverse_frame[i].state == REVERSE_STATE_VALUE)
511  new_reverse_frame[i].value += op2_as_short;
512  else
513  new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
514  }
515  reverse_frame_valid |= 1 << i;
516  dest_regs[i] = operands[0];
517  break;
518  case TILEGX_OPC_ADD:
519  if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
520  && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
521  {
522  /* We have values -- we can do this. */
523  new_reverse_frame[i] = reverse_frame[operands[2]];
524  new_reverse_frame[i].value
525  += reverse_frame[operands[i]].value;
526  }
527  else
528  {
529  /* We don't know anything about the values. Punt. */
530  new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
531  }
532  reverse_frame_valid |= 1 << i;
533  dest_regs[i] = operands[0];
534  break;
535  case TILEGX_OPC_MOVE:
536  new_reverse_frame[i] = reverse_frame[operands[1]];
537  reverse_frame_valid |= 1 << i;
538  dest_regs[i] = operands[0];
539  break;
540  case TILEGX_OPC_MOVEI:
541  case TILEGX_OPC_MOVELI:
542  new_reverse_frame[i].state = REVERSE_STATE_VALUE;
543  new_reverse_frame[i].value = operands[1];
544  reverse_frame_valid |= 1 << i;
545  dest_regs[i] = operands[0];
546  break;
547  case TILEGX_OPC_ORI:
548  if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE)
549  {
550  /* We have a value in A -- we can do this. */
551  new_reverse_frame[i] = reverse_frame[operands[1]];
552  new_reverse_frame[i].value
553  = reverse_frame[operands[1]].value | operands[2];
554  }
555  else if (operands[2] == 0)
556  {
557  /* This is a move. */
558  new_reverse_frame[i] = reverse_frame[operands[1]];
559  }
560  else
561  {
562  /* We don't know anything about the values. Punt. */
563  new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
564  }
565  reverse_frame_valid |= 1 << i;
566  dest_regs[i] = operands[0];
567  break;
568  case TILEGX_OPC_OR:
569  if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
570  && reverse_frame[operands[1]].value == 0)
571  {
572  /* This is a move. */
573  new_reverse_frame[i] = reverse_frame[operands[2]];
574  }
575  else if (reverse_frame[operands[2]].state == REVERSE_STATE_VALUE
576  && reverse_frame[operands[2]].value == 0)
577  {
578  /* This is a move. */
579  new_reverse_frame[i] = reverse_frame[operands[1]];
580  }
581  else
582  {
583  /* We don't know anything about the values. Punt. */
584  new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
585  }
586  reverse_frame_valid |= 1 << i;
587  dest_regs[i] = operands[0];
588  break;
589  case TILEGX_OPC_SUB:
590  if (reverse_frame[operands[1]].state == REVERSE_STATE_VALUE
591  && reverse_frame[operands[2]].state == REVERSE_STATE_VALUE)
592  {
593  /* We have values -- we can do this. */
594  new_reverse_frame[i] = reverse_frame[operands[1]];
595  new_reverse_frame[i].value
596  -= reverse_frame[operands[2]].value;
597  }
598  else
599  {
600  /* We don't know anything about the values. Punt. */
601  new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
602  }
603  reverse_frame_valid |= 1 << i;
604  dest_regs[i] = operands[0];
605  break;
606 
607  case TILEGX_OPC_FNOP:
608  case TILEGX_OPC_INFO:
609  case TILEGX_OPC_INFOL:
610  /* Nothing to see here, move on.
611  Note that real NOP is treated as a 'real' instruction
612  because someone must have intended that it be there.
613  It therefore terminates the prolog. */
614  break;
615 
616  case TILEGX_OPC_J:
617  case TILEGX_OPC_JAL:
618 
619  case TILEGX_OPC_BEQZ:
620  case TILEGX_OPC_BEQZT:
621  case TILEGX_OPC_BGEZ:
622  case TILEGX_OPC_BGEZT:
623  case TILEGX_OPC_BGTZ:
624  case TILEGX_OPC_BGTZT:
625  case TILEGX_OPC_BLBC:
626  case TILEGX_OPC_BLBCT:
627  case TILEGX_OPC_BLBS:
628  case TILEGX_OPC_BLBST:
629  case TILEGX_OPC_BLEZ:
630  case TILEGX_OPC_BLEZT:
631  case TILEGX_OPC_BLTZ:
632  case TILEGX_OPC_BLTZT:
633  case TILEGX_OPC_BNEZ:
634  case TILEGX_OPC_BNEZT:
635 
636  case TILEGX_OPC_IRET:
637  case TILEGX_OPC_JALR:
638  case TILEGX_OPC_JALRP:
639  case TILEGX_OPC_JR:
640  case TILEGX_OPC_JRP:
641  case TILEGX_OPC_SWINT0:
642  case TILEGX_OPC_SWINT1:
643  case TILEGX_OPC_SWINT2:
644  case TILEGX_OPC_SWINT3:
645  /* We're really done -- this is a branch. */
646  branch_seen = 1;
647  prolog_done = 1;
648  break;
649  default:
650  /* We don't know or care what this instruction is.
651  All we know is that it isn't part of a prolog, and if
652  there's a destination register, we're trashing it. */
653  prolog_done = 1;
654  for (j = 0; j < opcode->num_operands; j++)
655  {
656  if (this_insn->operands[j]->is_dest_reg)
657  {
658  dest_regs[i] = operands[j];
659  new_reverse_frame[i].state = REVERSE_STATE_UNKNOWN;
660  reverse_frame_valid |= 1 << i;
661  break;
662  }
663  }
664  break;
665  }
666  }
667 
668  /* Now update the reverse frames. */
669  for (i = 0; i < num_insns; i++)
670  {
671  /* ISSUE: Does this properly handle "network" registers? */
672  if ((reverse_frame_valid & (1 << i))
673  && dest_regs[i] != TILEGX_ZERO_REGNUM)
674  reverse_frame[dest_regs[i]] = new_reverse_frame[i];
675  }
676 
677  if (prev_sp_value != 0)
678  {
679  /* GCC uses R52 as a frame pointer. Have we seen "move r52, sp"? */
680  if (reverse_frame[TILEGX_R52_REGNUM].state == REVERSE_STATE_REGISTER
681  && reverse_frame[TILEGX_R52_REGNUM].value == TILEGX_SP_REGNUM)
682  {
684  reverse_frame[TILEGX_R52_REGNUM].value = prev_sp_value;
685  }
686 
687  prev_sp_value = 0;
688  }
689 
690  if (prolog_done && prolog_end == end_addr)
691  {
692  /* We found non-prolog code. As such, _this_ instruction
693  is the one after the prolog. We keep processing, because
694  there may be more prolog code in there, but this is what
695  we'll return. */
696  /* ISSUE: There may not have actually been a prologue, and
697  we may have simply skipped some random instructions. */
698  prolog_end = next_addr;
699  }
700  if (branch_seen)
701  {
702  /* We saw a branch. The prolog absolutely must be over. */
703  break;
704  }
705  }
706 
707  if (prolog_end == end_addr && cache)
708  {
709  /* We may have terminated the prolog early, and we're certainly
710  at THIS point right now. It's possible that the values of
711  registers we need are currently actually in other registers
712  (and haven't been written to memory yet). Go find them. */
713  for (i = 0; i < TILEGX_NUM_PHYS_REGS; i++)
714  {
715  if (reverse_frame[i].state == REVERSE_STATE_REGISTER
716  && reverse_frame[i].value != i)
717  {
718  unsigned saved_register = (unsigned) reverse_frame[i].value;
719 
720  cache->saved_regs[saved_register].realreg = i;
721  cache->saved_regs[saved_register].addr = (LONGEST) -1;
722  }
723  }
724  }
725 
726  if (lr_saved_on_stack_p)
727  {
731  }
732 
733  return prolog_end;
734 }
735 
736 /* This is the implementation of gdbarch method skip_prologue. */
737 
738 static CORE_ADDR
740 {
741  CORE_ADDR func_start, end_pc;
742  struct obj_section *s;
743 
744  /* This is the preferred method, find the end of the prologue by
745  using the debugging information. */
746  if (find_pc_partial_function (start_pc, NULL, &func_start, NULL))
747  {
748  CORE_ADDR post_prologue_pc
749  = skip_prologue_using_sal (gdbarch, func_start);
750 
751  if (post_prologue_pc != 0)
752  return std::max (start_pc, post_prologue_pc);
753  }
754 
755  /* Don't straddle a section boundary. */
756  s = find_pc_section (start_pc);
757  end_pc = start_pc + 8 * TILEGX_BUNDLE_SIZE_IN_BYTES;
758  if (s != NULL)
759  end_pc = std::min (end_pc, obj_section_endaddr (s));
760 
761  /* Otherwise, try to skip prologue the hard way. */
763  start_pc,
764  end_pc,
765  NULL, NULL);
766 }
767 
768 /* This is the implementation of gdbarch method stack_frame_destroyed_p. */
769 
770 static int
772 {
773  CORE_ADDR func_addr = 0, func_end = 0;
774 
775  if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
776  {
777  CORE_ADDR addr = func_end - TILEGX_BUNDLE_SIZE_IN_BYTES;
778 
779  /* FIXME: Find the actual epilogue. */
780  /* HACK: Just assume the final bundle is the "ret" instruction". */
781  if (pc > addr)
782  return 1;
783  }
784  return 0;
785 }
786 
787 /* This is the implementation of gdbarch method get_longjmp_target. */
788 
789 static int
791 {
792  struct gdbarch *gdbarch = get_frame_arch (frame);
793  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
794  CORE_ADDR jb_addr;
795  gdb_byte buf[8];
796 
798 
799  /* TileGX jmp_buf contains 32 elements of type __uint_reg_t which
800  has a size of 8 bytes. The return address is stored in the 25th
801  slot. */
802  if (target_read_memory (jb_addr + 25 * 8, buf, 8))
803  return 0;
804 
805  *pc = extract_unsigned_integer (buf, 8, byte_order);
806 
807  return 1;
808 }
809 
810 /* by assigning the 'faultnum' reg in kernel pt_regs with this value,
811  kernel do_signal will not check r0. see tilegx kernel/signal.c
812  for details. */
813 #define INT_SWINT_1_SIGRETURN (~0)
814 
815 /* Implement the "write_pc" gdbarch method. */
816 
817 static void
819 {
821 
822  /* We must be careful with modifying the program counter. If we
823  just interrupted a system call, the kernel might try to restart
824  it when we resume the inferior. On restarting the system call,
825  the kernel will try backing up the program counter even though it
826  no longer points at the system call. This typically results in a
827  SIGSEGV or SIGILL. We can prevent this by writing INT_SWINT_1_SIGRETURN
828  in the "faultnum" pseudo-register.
829 
830  Note that "faultnum" is saved when setting up a dummy call frame.
831  This means that it is properly restored when that frame is
832  popped, and that the interrupted system call will be restarted
833  when we resume the inferior on return from a function call from
834  within GDB. In all other cases the system call will not be
835  restarted. */
838 }
839 
840 /* 64-bit pattern for a { bpt ; nop } bundle. */
842  { 0x00, 0x50, 0x48, 0x51, 0xae, 0x44, 0x6a, 0x28 };
843 
844 typedef BP_MANIPULATION (tilegx_break_insn) tilegx_breakpoint;
845 
846 /* Normal frames. */
847 
848 static struct tilegx_frame_cache *
849 tilegx_frame_cache (struct frame_info *this_frame, void **this_cache)
850 {
851  struct gdbarch *gdbarch = get_frame_arch (this_frame);
852  struct tilegx_frame_cache *cache;
853  CORE_ADDR current_pc;
854 
855  if (*this_cache)
856  return (struct tilegx_frame_cache *) *this_cache;
857 
858  cache = FRAME_OBSTACK_ZALLOC (struct tilegx_frame_cache);
859  *this_cache = cache;
860  cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
861  cache->base = 0;
862  cache->start_pc = get_frame_func (this_frame);
863  current_pc = get_frame_pc (this_frame);
864 
865  cache->base = get_frame_register_unsigned (this_frame, TILEGX_SP_REGNUM);
867 
868  if (cache->start_pc)
869  tilegx_analyze_prologue (gdbarch, cache->start_pc, current_pc,
870  cache, this_frame);
871 
873 
874  return cache;
875 }
876 
877 /* Retrieve the value of REGNUM in FRAME. */
878 
879 static struct value*
881  void **this_cache,
882  int regnum)
883 {
884  struct tilegx_frame_cache *info =
885  tilegx_frame_cache (this_frame, this_cache);
886 
887  return trad_frame_get_prev_register (this_frame, info->saved_regs,
888  regnum);
889 }
890 
891 /* Build frame id. */
892 
893 static void
894 tilegx_frame_this_id (struct frame_info *this_frame, void **this_cache,
895  struct frame_id *this_id)
896 {
897  struct tilegx_frame_cache *info =
898  tilegx_frame_cache (this_frame, this_cache);
899 
900  /* This marks the outermost frame. */
901  if (info->base == 0)
902  return;
903 
904  (*this_id) = frame_id_build (info->base, info->start_pc);
905 }
906 
907 static CORE_ADDR
908 tilegx_frame_base_address (struct frame_info *this_frame, void **this_cache)
909 {
910  struct tilegx_frame_cache *cache =
911  tilegx_frame_cache (this_frame, this_cache);
912 
913  return cache->base;
914 }
915 
916 static const struct frame_unwind tilegx_frame_unwind = {
917  NORMAL_FRAME,
921  NULL, /* const struct frame_data *unwind_data */
922  default_frame_sniffer, /* frame_sniffer_ftype *sniffer */
923  NULL /* frame_prev_pc_ftype *prev_pc */
924 };
925 
926 static const struct frame_base tilegx_frame_base = {
931 };
932 
933 static CORE_ADDR
934 tilegx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
935 {
937 }
938 
939 static CORE_ADDR
940 tilegx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
941 {
943 }
944 
945 static struct frame_id
947  struct frame_info *this_frame)
948 {
949  CORE_ADDR sp;
950 
952  return frame_id_build (sp, get_frame_pc (this_frame));
953 }
954 
955 
956 /* We cannot read/write the "special" registers. */
957 
958 static int
960 {
961  if (regno >= 0 && regno < TILEGX_NUM_EASY_REGS)
962  return 0;
963  else if (regno == TILEGX_PC_REGNUM
964  || regno == TILEGX_FAULTNUM_REGNUM)
965  return 0;
966  else
967  return 1;
968 }
969 
970 static struct gdbarch *
971 tilegx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
972 {
973  struct gdbarch *gdbarch;
974  int arch_size = 64;
975 
976  /* Handle arch_size == 32 or 64. Default to 64. */
977  if (info.abfd)
978  arch_size = bfd_get_arch_size (info.abfd);
979 
980  /* Try to find a pre-existing architecture. */
981  for (arches = gdbarch_list_lookup_by_info (arches, &info);
982  arches != NULL;
983  arches = gdbarch_list_lookup_by_info (arches->next, &info))
984  {
985  /* We only have two flavors -- just make sure arch_size matches. */
986  if (gdbarch_ptr_bit (arches->gdbarch) == arch_size)
987  return (arches->gdbarch);
988  }
989 
990  gdbarch = gdbarch_alloc (&info, NULL);
991 
992  /* Basic register fields and methods, datatype sizes and stuff. */
993 
994  /* There are 64 physical registers which can be referenced by
995  instructions (although only 56 of them can actually be
996  debugged) and 1 magic register (the PC). The other three
997  magic registers (ex1, syscall, orig_r0) which are known to
998  "ptrace" are ignored by "gdb". Note that we simply pretend
999  that there are 65 registers, and no "pseudo registers". */
1002 
1005 
1008 
1011  set_gdbarch_long_bit (gdbarch, arch_size);
1013 
1017 
1018  set_gdbarch_ptr_bit (gdbarch, arch_size);
1019  set_gdbarch_addr_bit (gdbarch, arch_size);
1020 
1025 
1026  /* Stack grows down. */
1028 
1029  /* Frame Info. */
1035 
1037 
1039 
1040  /* Map debug registers into internal register numbers. */
1042 
1043  /* These values and methods are used when gdb calls a target function. */
1048  tilegx_breakpoint::kind_from_pc);
1050  tilegx_breakpoint::bp_from_kind);
1052 
1053  gdbarch_init_osabi (info, gdbarch);
1054 
1057 
1058  return gdbarch;
1059 }
1060 
1061 void
1063 {
1064  register_gdbarch_init (bfd_arch_tilegx, tilegx_gdbarch_init);
1065 }
static struct gdbarch * tilegx_gdbarch_init(struct gdbarch_info info, struct gdbarch_list *arches)
Definition: tilegx-tdep.c:971
void set_gdbarch_num_regs(struct gdbarch *gdbarch, int num_regs)
Definition: gdbarch.c:2050
void set_gdbarch_double_bit(struct gdbarch *gdbarch, int double_bit)
Definition: gdbarch.c:1723
void set_gdbarch_frame_align(struct gdbarch *gdbarch, gdbarch_frame_align_ftype frame_align)
Definition: gdbarch.c:3151
void set_gdbarch_cannot_fetch_register(struct gdbarch *gdbarch, gdbarch_cannot_fetch_register_ftype cannot_fetch_register)
Definition: gdbarch.c:2531
static CORE_ADDR tilegx_frame_base_address(struct frame_info *this_frame, void **this_cache)
Definition: tilegx-tdep.c:908
struct frame_id frame_id_build(CORE_ADDR stack_addr, CORE_ADDR code_addr)
Definition: frame.c:624
void set_gdbarch_get_longjmp_target(struct gdbarch *gdbarch, gdbarch_get_longjmp_target_ftype get_longjmp_target)
Definition: gdbarch.c:2572
static CORE_ADDR tilegx_push_dummy_call(struct gdbarch *gdbarch, struct value *function, struct regcache *regcache, CORE_ADDR bp_addr, int nargs, struct value **args, CORE_ADDR sp, int struct_return, CORE_ADDR struct_addr)
Definition: tilegx-tdep.c:279
struct type * builtin_func_ptr
Definition: gdbtypes.h:1565
void set_gdbarch_float_bit(struct gdbarch *gdbarch, int float_bit)
Definition: gdbarch.c:1690
CORE_ADDR get_frame_pc(struct frame_info *frame)
Definition: frame.c:2376
bfd_vma CORE_ADDR
Definition: common-types.h:41
static int tilegx_cannot_reference_register(struct gdbarch *gdbarch, int regno)
Definition: tilegx-tdep.c:959
void gdbarch_init_osabi(struct gdbarch_info info, struct gdbarch *gdbarch)
Definition: osabi.c:334
struct value * trad_frame_get_prev_register(struct frame_info *this_frame, struct trad_frame_saved_reg this_saved_regs[], int regnum)
Definition: trad-frame.c:142
static int tilegx_dwarf2_reg_to_regnum(struct gdbarch *gdbarch, int num)
Definition: tilegx-tdep.c:180
void trad_frame_set_value(struct trad_frame_saved_reg this_saved_regs[], int regnum, LONGEST val)
Definition: trad-frame.c:99
void set_gdbarch_write_pc(struct gdbarch *gdbarch, gdbarch_write_pc_ftype write_pc)
Definition: gdbarch.c:1943
int gdbarch_ptr_bit(struct gdbarch *gdbarch)
Definition: gdbarch.c:1831
struct type * value_enclosing_type(const struct value *value)
Definition: value.c:1175
ULONGEST frame_unwind_register_unsigned(struct frame_info *frame, int regnum)
Definition: frame.c:1279
void set_gdbarch_short_bit(struct gdbarch *gdbarch, int short_bit)
Definition: gdbarch.c:1572
static void tilegx_extract_return_value(struct type *type, struct regcache *regcache, gdb_byte *valbuf)
Definition: tilegx-tdep.c:215
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition: gdbtypes.c:5217
void * memset(T *s, int c, size_t n)=delete
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
#define obj_section_endaddr(s)
Definition: objfiles.h:146
return_value_convention
Definition: defs.h:247
static CORE_ADDR tilegx_skip_prologue(struct gdbarch *gdbarch, CORE_ADDR start_pc)
Definition: tilegx-tdep.c:739
struct gdbarch_list * gdbarch_list_lookup_by_info(struct gdbarch_list *arches, const struct gdbarch_info *info)
Definition: gdbarch.c:5309
CORE_ADDR skip_prologue_using_sal(struct gdbarch *gdbarch, CORE_ADDR func_addr)
Definition: symtab.c:3854
struct gdbarch_list * next
Definition: gdbarch.h:1623
static CORE_ADDR tilegx_frame_align(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition: tilegx-tdep.c:270
static const struct frame_unwind tilegx_frame_unwind
Definition: tilegx-tdep.c:916
void set_gdbarch_dwarf2_reg_to_regnum(struct gdbarch *gdbarch, gdbarch_dwarf2_reg_to_regnum_ftype dwarf2_reg_to_regnum)
Definition: gdbarch.c:2275
static void tilegx_write_pc(struct regcache *regcache, CORE_ADDR pc)
Definition: tilegx-tdep.c:818
void frame_unwind_append_unwinder(struct gdbarch *gdbarch, const struct frame_unwind *unwinder)
Definition: frame-unwind.c:79
#define FRAME_OBSTACK_ZALLOC(TYPE)
Definition: frame.h:678
void set_gdbarch_addr_bit(struct gdbarch *gdbarch, int addr_bit)
Definition: gdbarch.c:1859
void memory_error(enum target_xfer_status err, CORE_ADDR memaddr)
Definition: corefile.c:205
void frame_base_set_default(struct gdbarch *gdbarch, const struct frame_base *default_base)
Definition: frame-base.c:95
reverse_state
Definition: tilegx-tdep.c:57
static struct frame_id tilegx_unwind_dummy_id(struct gdbarch *gdbarch, struct frame_info *this_frame)
Definition: tilegx-tdep.c:946
void set_gdbarch_register_type(struct gdbarch *gdbarch, gdbarch_register_type_ftype register_type)
Definition: gdbarch.c:2316
const gdb_byte * value_contents(struct value *value)
Definition: value.c:1407
static const struct frame_base tilegx_frame_base
Definition: tilegx-tdep.c:926
void set_gdbarch_stack_frame_destroyed_p(struct gdbarch *gdbarch, gdbarch_stack_frame_destroyed_p_ftype stack_frame_destroyed_p)
Definition: gdbarch.c:3367
static CORE_ADDR tilegx_unwind_sp(struct gdbarch *gdbarch, struct frame_info *next_frame)
Definition: tilegx-tdep.c:934
static ULONGEST extract_unsigned_integer(const gdb_byte *addr, int len, enum bfd_endian byte_order)
Definition: defs.h:577
int safe_frame_unwind_memory(struct frame_info *this_frame, CORE_ADDR addr, gdb_byte *buf, int len)
Definition: frame.c:2681
static CORE_ADDR tilegx_analyze_prologue(struct gdbarch *gdbarch, CORE_ADDR start_addr, CORE_ADDR end_addr, struct tilegx_frame_cache *cache, struct frame_info *next_frame)
Definition: tilegx-tdep.c:366
void set_gdbarch_sp_regnum(struct gdbarch *gdbarch, int sp_regnum)
Definition: gdbarch.c:2156
bfd * abfd
Definition: gdbarch.h:1637
void set_gdbarch_dummy_id(struct gdbarch *gdbarch, gdbarch_dummy_id_ftype dummy_id)
Definition: gdbarch.c:2340
typedef BP_MANIPULATION(tilegx_break_insn)
Definition: tilegx-tdep.c:844
struct_return
Definition: arm-tdep.h:88
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition: gdbarch.c:1509
void set_gdbarch_cannot_store_register(struct gdbarch *gdbarch, gdbarch_cannot_store_register_ftype cannot_store_register)
Definition: gdbarch.c:2548
#define TARGET_CHAR_BIT
Definition: host-defs.h:29
Definition: gdbtypes.h:749
int find_pc_partial_function(CORE_ADDR pc, const char **name, CORE_ADDR *address, CORE_ADDR *endaddr)
Definition: blockframe.c:320
enum reverse_state state
Definition: tilegx-tdep.c:68
void _initialize_tilegx_tdep(void)
Definition: tilegx-tdep.c:1062
void set_gdbarch_unwind_pc(struct gdbarch *gdbarch, gdbarch_unwind_pc_ftype unwind_pc)
Definition: gdbarch.c:3079
int default_frame_sniffer(const struct frame_unwind *self, struct frame_info *this_frame, void **this_prologue_cache)
Definition: frame-unwind.c:174
static enum return_value_convention tilegx_return_value(struct gdbarch *gdbarch, struct value *function, struct type *type, struct regcache *regcache, gdb_byte *readbuf, const gdb_byte *writebuf)
Definition: tilegx-tdep.c:254
struct trad_frame_saved_reg * saved_regs
Definition: tilegx-tdep.c:53
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition: gnu-nat.c:1822
static int tilegx_get_longjmp_target(struct frame_info *frame, CORE_ADDR *pc)
Definition: tilegx-tdep.c:790
void set_gdbarch_unwind_sp(struct gdbarch *gdbarch, gdbarch_unwind_sp_ftype unwind_sp)
Definition: gdbarch.c:3103
static void tilegx_store_return_value(struct type *type, struct regcache *regcache, const void *valbuf)
Definition: tilegx-tdep.c:230
struct gdbarch * gdbarch
Definition: gdbarch.h:1622
int regnum
Definition: aarch64-tdep.c:77
struct obj_section * find_pc_section(CORE_ADDR pc)
Definition: objfiles.c:1395
ULONGEST get_frame_register_unsigned(struct frame_info *frame, int regnum)
Definition: frame.c:1308
void set_gdbarch_breakpoint_kind_from_pc(struct gdbarch *gdbarch, gdbarch_breakpoint_kind_from_pc_ftype breakpoint_kind_from_pc)
Definition: gdbarch.c:2871
struct trad_frame_saved_reg * trad_frame_alloc_saved_regs(struct gdbarch *gdbarch)
Definition: trad-frame.c:47
void set_gdbarch_long_long_bit(struct gdbarch *gdbarch, int long_long_bit)
Definition: gdbarch.c:1623
Definition: value.c:169
static void tilegx_frame_this_id(struct frame_info *this_frame, void **this_cache, struct frame_id *this_id)
Definition: tilegx-tdep.c:894
static CORE_ADDR tilegx_unwind_pc(struct gdbarch *gdbarch, struct frame_info *next_frame)
Definition: tilegx-tdep.c:940
int core_addr_lessthan(CORE_ADDR lhs, CORE_ADDR rhs)
Definition: arch-utils.c:117
bfd_byte gdb_byte
Definition: common-types.h:38
ULONGEST align_up(ULONGEST v, int n)
Definition: utils.c:2997
static struct value * tilegx_frame_prev_register(struct frame_info *this_frame, void **this_cache, int regnum)
Definition: tilegx-tdep.c:880
static int tilegx_use_struct_convention(struct type *type)
Definition: tilegx-tdep.c:202
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
enum register_status regcache_raw_read(struct regcache *regcache, int regnum, gdb_byte *buf)
Definition: regcache.c:565
void regcache_cooked_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition: regcache.c:806
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: target.c:1370
void set_gdbarch_int_bit(struct gdbarch *gdbarch, int int_bit)
Definition: gdbarch.c:1589
static struct type * tilegx_register_type(struct gdbarch *gdbarch, int regnum)
Definition: tilegx-tdep.c:169
constexpr gdb_byte tilegx_break_insn[]
Definition: tilegx-tdep.c:841
CORE_ADDR start_pc
Definition: tilegx-tdep.c:50
void set_gdbarch_num_pseudo_regs(struct gdbarch *gdbarch, int num_pseudo_regs)
Definition: gdbarch.c:2067
void dwarf2_append_unwinders(struct gdbarch *gdbarch)
static int tilegx_type_is_scalar(struct type *t)
Definition: tilegx-tdep.c:189
gdb::def_vector< gdb_byte > byte_vector
Definition: byte-vector.h:58
static const struct tilegx_reverse_regs template_reverse_regs[TILEGX_NUM_PHYS_REGS]
Definition: tilegx-tdep.c:72
#define INT_SWINT_1_SIGRETURN
Definition: tilegx-tdep.c:813
unsigned long long ULONGEST
Definition: common-types.h:53
enum unwind_stop_reason default_frame_unwind_stop_reason(struct frame_info *this_frame, void **this_cache)
Definition: frame-unwind.c:184
void set_gdbarch_long_double_bit(struct gdbarch *gdbarch, int long_double_bit)
Definition: gdbarch.c:1756
void set_gdbarch_long_bit(struct gdbarch *gdbarch, int long_bit)
Definition: gdbarch.c:1606
void set_gdbarch_return_value(struct gdbarch *gdbarch, gdbarch_return_value_ftype return_value)
Definition: gdbarch.c:2738
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1235
void set_gdbarch_ptr_bit(struct gdbarch *gdbarch, int ptr_bit)
Definition: gdbarch.c:1841
void set_gdbarch_push_dummy_call(struct gdbarch *gdbarch, gdbarch_push_dummy_call_ftype push_dummy_call)
Definition: gdbarch.c:2381
static int tilegx_stack_frame_destroyed_p(struct gdbarch *gdbarch, CORE_ADDR pc)
Definition: tilegx-tdep.c:771
struct type * builtin_uint64
Definition: gdbtypes.h:1541
void set_gdbarch_sw_breakpoint_from_kind(struct gdbarch *gdbarch, gdbarch_sw_breakpoint_from_kind_ftype sw_breakpoint_from_kind)
Definition: gdbarch.c:2888
void register_gdbarch_init(enum bfd_architecture bfd_architecture, gdbarch_init_ftype *init)
Definition: gdbarch.c:5299
void write_memory(CORE_ADDR memaddr, const bfd_byte *myaddr, ssize_t len)
Definition: corefile.c:394
void set_gdbarch_skip_prologue(struct gdbarch *gdbarch, gdbarch_skip_prologue_ftype skip_prologue)
Definition: gdbarch.c:2772
enum bfd_endian byte_order
Definition: gdbarch.c:137
static const char * tilegx_register_name(struct gdbarch *gdbarch, int regnum)
Definition: tilegx-tdep.c:143
void set_gdbarch_pc_regnum(struct gdbarch *gdbarch, int pc_regnum)
Definition: gdbarch.c:2173
void set_gdbarch_register_name(struct gdbarch *gdbarch, gdbarch_register_name_ftype register_name)
Definition: gdbarch.c:2292
CORE_ADDR get_frame_func(struct frame_info *this_frame)
Definition: frame.c:1001
struct gdbarch * gdbarch_alloc(const struct gdbarch_info *info, struct gdbarch_tdep *tdep)
Definition: gdbarch.c:361
void set_gdbarch_inner_than(struct gdbarch *gdbarch, gdbarch_inner_than_ftype inner_than)
Definition: gdbarch.c:2837
struct gdbarch * get_frame_arch(struct frame_info *this_frame)
Definition: frame.c:2691
long long LONGEST
Definition: common-types.h:52
void regcache_raw_write(struct regcache *regcache, int regnum, const gdb_byte *buf)
Definition: regcache.c:831