OpenOCD
arc_cmd.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2013-2015,2019-2020 Synopsys, Inc. *
5  * Frank Dols <frank.dols@synopsys.com> *
6  * Mischa Jonker <mischa.jonker@synopsys.com> *
7  * Anton Kolesov <anton.kolesov@synopsys.com> *
8  * Evgeniy Didin <didin@synopsys.com> *
9  ***************************************************************************/
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 #include "arc.h"
16 
17 /* --------------------------------------------------------------------------
18  *
19  * ARC targets expose command interface.
20  * It can be accessed via GDB through the (gdb) monitor command.
21  *
22  * ------------------------------------------------------------------------- */
23 
24 
25 static int arc_cmd_jim_get_uint32(struct jim_getopt_info *goi, uint32_t *value)
26 {
27  jim_wide value_wide;
28  JIM_CHECK_RETVAL(jim_getopt_wide(goi, &value_wide));
29  *value = (uint32_t)value_wide;
30  return JIM_OK;
31 }
32 
36 };
37 /* Add flags register data type */
41 };
42 
43 static struct jim_nvp nvp_add_reg_type_flags_opts[] = {
44  { .name = "-name", .value = CFG_ADD_REG_TYPE_FLAGS_NAME },
45  { .name = "-flag", .value = CFG_ADD_REG_TYPE_FLAGS_FLAG },
46  { .name = NULL, .value = -1 }
47 };
48 
49 /* Helper function to check if all field required for register
50  * are set up */
51 static const char *validate_register(const struct arc_reg_desc * const reg, bool arch_num_set)
52 {
53  /* Check that required fields are set */
54  if (!reg->name)
55  return "-name option is required";
56  if (!reg->gdb_xml_feature)
57  return "-feature option is required";
58  if (!arch_num_set)
59  return "-num option is required";
60  if (reg->is_bcr && reg->is_core)
61  return "Register cannot be both -core and -bcr.";
62  return NULL;
63 }
64 
65 /* Helper function to read the name of register type or register from
66  * configure files */
68  const char **name, int *name_len)
69 {
70  int e = JIM_OK;
71 
72  if (!goi->argc) {
73  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-name <name> ...");
74  return JIM_ERR;
75  }
76  e = jim_getopt_string(goi, name, name_len);
77  return e;
78 }
79 
80 /* Helper function to read bitfields/flags of register type. */
81 static int jim_arc_read_reg_type_field(struct jim_getopt_info *goi, const char **field_name, int *field_name_len,
82  struct arc_reg_bitfield *bitfields, int cur_field, int type)
83 {
84  jim_wide start_pos, end_pos;
85 
86  int e = JIM_OK;
87  if ((type == CFG_ADD_REG_TYPE_STRUCT && goi->argc < 3) ||
88  (type == CFG_ADD_REG_TYPE_FLAG && goi->argc < 2)) {
89  Jim_SetResultFormatted(goi->interp, "Not enough arguments after -flag/-bitfield");
90  return JIM_ERR;
91  }
92 
93  e = jim_getopt_string(goi, field_name, field_name_len);
94  if (e != JIM_OK)
95  return e;
96 
97  /* read start position of bitfield/flag */
98  e = jim_getopt_wide(goi, &start_pos);
99  if (e != JIM_OK)
100  return e;
101 
102  end_pos = start_pos;
103 
104  /* Check if any arguments remain,
105  * set bitfields[cur_field].end if flag is multibit */
106  if (goi->argc > 0)
107  /* Check current argv[0], if it is equal to "-flag",
108  * than bitfields[cur_field].end remains start */
109  if ((strcmp(Jim_String(goi->argv[0]), "-flag") && type == CFG_ADD_REG_TYPE_FLAG)
110  || (type == CFG_ADD_REG_TYPE_STRUCT)) {
111  e = jim_getopt_wide(goi, &end_pos);
112  if (e != JIM_OK) {
113  Jim_SetResultFormatted(goi->interp, "Error reading end position");
114  return e;
115  }
116  }
117 
118  bitfields[cur_field].bitfield.start = start_pos;
119  bitfields[cur_field].bitfield.end = end_pos;
120  if ((end_pos != start_pos) || (type == CFG_ADD_REG_TYPE_STRUCT))
121  bitfields[cur_field].bitfield.type = REG_TYPE_INT;
122  return e;
123 }
124 
125 static int jim_arc_add_reg_type_flags(Jim_Interp *interp, int argc,
126  Jim_Obj * const *argv)
127 {
128  struct jim_getopt_info goi;
130 
131  LOG_DEBUG("-");
132 
133  struct command_context *ctx;
134  struct target *target;
135 
136  ctx = current_command_context(interp);
137  assert(ctx);
138  target = get_current_target(ctx);
139  if (!target) {
140  Jim_SetResultFormatted(goi.interp, "No current target");
141  return JIM_ERR;
142  }
143 
144  int e = JIM_OK;
145 
146  /* Check if the amount of arguments is not zero */
147  if (goi.argc <= 0) {
148  Jim_SetResultFormatted(goi.interp, "The command has no arguments");
149  return JIM_ERR;
150  }
151 
152  /* Estimate number of registers as (argc - 2)/3 as each -flag option has 2
153  * arguments while -name is required. */
154  unsigned int fields_sz = (goi.argc - 2) / 3;
155  unsigned int cur_field = 0;
156 
157  /* The maximum amount of bitfields is 32 */
158  if (fields_sz > 32) {
159  Jim_SetResultFormatted(goi.interp, "The amount of bitfields exceed 32");
160  return JIM_ERR;
161  }
162 
163  struct arc_reg_data_type *type = calloc(1, sizeof(*type));
164  struct reg_data_type_flags *flags = &type->data_type_flags;
165  struct reg_data_type_flags_field *fields = calloc(fields_sz, sizeof(*fields));
166  type->reg_type_flags_field = fields;
167  struct arc_reg_bitfield *bitfields = calloc(fields_sz, sizeof(*bitfields));
168  if (!(type && fields && bitfields)) {
169  Jim_SetResultFormatted(goi.interp, "Failed to allocate memory.");
170  goto fail;
171  }
172 
173  /* Initialize type */
174  type->bitfields = bitfields;
175  type->data_type.id = type->data_type_id;
176  type->data_type.type = REG_TYPE_ARCH_DEFINED;
177  type->data_type.type_class = REG_TYPE_CLASS_FLAGS;
178  type->data_type.reg_type_flags = flags;
179  flags->size = 4; /* For now ARC has only 32-bit registers */
180 
181  while (goi.argc > 0 && e == JIM_OK) {
182  struct jim_nvp *n;
184  if (e != JIM_OK) {
186  continue;
187  }
188 
189  switch (n->value) {
191  {
192  const char *name = NULL;
193  int name_len = 0;
194 
195  e = jim_arc_read_reg_name_field(&goi, &name, &name_len);
196  if (e != JIM_OK) {
197  Jim_SetResultFormatted(goi.interp, "Unable to read reg name.");
198  goto fail;
199  }
200 
201  if (name_len > REG_TYPE_MAX_NAME_LENGTH) {
202  Jim_SetResultFormatted(goi.interp, "Reg type name is too big.");
203  goto fail;
204  }
205 
206  strncpy((void *)type->data_type.id, name, name_len);
207  if (!type->data_type.id) {
208  Jim_SetResultFormatted(goi.interp, "Unable to setup reg type name.");
209  goto fail;
210  }
211 
212  break;
213  }
214 
216  {
217  const char *field_name = NULL;
218  int field_name_len = 0;
219 
220  e = jim_arc_read_reg_type_field(&goi, &field_name, &field_name_len, bitfields,
221  cur_field, CFG_ADD_REG_TYPE_FLAG);
222  if (e != JIM_OK) {
223  Jim_SetResultFormatted(goi.interp, "Unable to add reg_type_flag field.");
224  goto fail;
225  }
226 
227  if (field_name_len > REG_TYPE_MAX_NAME_LENGTH) {
228  Jim_SetResultFormatted(goi.interp, "Reg type field_name_len is too big.");
229  goto fail;
230  }
231 
232  fields[cur_field].name = bitfields[cur_field].name;
233  strncpy(bitfields[cur_field].name, field_name, field_name_len);
234  if (!fields[cur_field].name) {
235  Jim_SetResultFormatted(goi.interp, "Unable to setup field name. ");
236  goto fail;
237  }
238 
239  fields[cur_field].bitfield = &(bitfields[cur_field].bitfield);
240  if (cur_field > 0)
241  fields[cur_field - 1].next = &(fields[cur_field]);
242  else
243  flags->fields = fields;
244 
245  cur_field += 1;
246  break;
247  }
248  }
249  }
250 
251  if (!type->data_type.id) {
252  Jim_SetResultFormatted(goi.interp, "-name is a required option");
253  goto fail;
254  }
255 
257 
258  LOG_DEBUG("added flags type {name=%s}", type->data_type.id);
259 
260  return JIM_OK;
261 fail:
262  free(type);
263  free(fields);
264  free(bitfields);
265 
266  return JIM_ERR;
267 }
268 
269 /* Add struct register data type */
273 };
274 
275 static struct jim_nvp nvp_add_reg_type_struct_opts[] = {
276  { .name = "-name", .value = CFG_ADD_REG_TYPE_STRUCT_NAME },
277  { .name = "-bitfield", .value = CFG_ADD_REG_TYPE_STRUCT_BITFIELD },
278  { .name = NULL, .value = -1 }
279 };
280 
281 static int jim_arc_set_aux_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
282 {
283 
284  struct command_context *context;
285  struct target *target;
286  uint32_t regnum;
287  uint32_t value;
288 
289  struct jim_getopt_info goi;
291 
292  if (goi.argc != 2) {
293  Jim_SetResultFormatted(goi.interp,
294  "usage: %s <aux_reg_num> <aux_reg_value>", Jim_GetString(argv[0], NULL));
295  return JIM_ERR;
296  }
297 
298  context = current_command_context(interp);
299  assert(context);
300 
301  target = get_current_target(context);
302  if (!target) {
303  Jim_SetResultFormatted(goi.interp, "No current target");
304  return JIM_ERR;
305  }
306 
307  /* Register number */
309 
310  /* Register value */
312 
313  struct arc_common *arc = target_to_arc(target);
314  assert(arc);
315 
316  CHECK_RETVAL(arc_jtag_write_aux_reg_one(&arc->jtag_info, regnum, value));
317 
318  return ERROR_OK;
319 }
320 
321 static int jim_arc_get_aux_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
322 {
323  struct command_context *context;
324  struct target *target;
325  uint32_t regnum;
326  uint32_t value;
327 
328  struct jim_getopt_info goi;
330 
331  if (goi.argc != 1) {
332  Jim_SetResultFormatted(goi.interp,
333  "usage: %s <aux_reg_num>", Jim_GetString(argv[0], NULL));
334  return JIM_ERR;
335  }
336 
337  context = current_command_context(interp);
338  assert(context);
339 
340  target = get_current_target(context);
341  if (!target) {
342  Jim_SetResultFormatted(goi.interp, "No current target");
343  return JIM_ERR;
344  }
345 
346  /* Register number */
348 
349  struct arc_common *arc = target_to_arc(target);
350  assert(arc);
351 
352  CHECK_RETVAL(arc_jtag_read_aux_reg_one(&arc->jtag_info, regnum, &value));
353  Jim_SetResultInt(interp, value);
354 
355  return ERROR_OK;
356 }
357 
358 static int jim_arc_get_core_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
359 {
360  struct command_context *context;
361  struct target *target;
362  uint32_t regnum;
363  uint32_t value;
364 
365  struct jim_getopt_info goi;
367 
368  if (goi.argc != 1) {
369  Jim_SetResultFormatted(goi.interp,
370  "usage: %s <core_reg_num>", Jim_GetString(argv[0], NULL));
371  return JIM_ERR;
372  }
373 
374  context = current_command_context(interp);
375  assert(context);
376 
377  target = get_current_target(context);
378  if (!target) {
379  Jim_SetResultFormatted(goi.interp, "No current target");
380  return JIM_ERR;
381  }
382 
383  /* Register number */
385  if (regnum > CORE_REG_MAX_NUMBER || regnum == ARC_R61 || regnum == ARC_R62) {
386  Jim_SetResultFormatted(goi.interp, "Core register number %i "
387  "is invalid. Must less then 64 and not 61 and 62.", regnum);
388  return JIM_ERR;
389  }
390 
391  struct arc_common *arc = target_to_arc(target);
392  assert(arc);
393 
394  /* Read value */
395  CHECK_RETVAL(arc_jtag_read_core_reg_one(&arc->jtag_info, regnum, &value));
396  Jim_SetResultInt(interp, value);
397 
398  return ERROR_OK;
399 }
400 
401 static int jim_arc_set_core_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
402 {
403  struct command_context *context;
404  struct target *target;
405  uint32_t regnum;
406  uint32_t value;
407 
408  struct jim_getopt_info goi;
410 
411  if (goi.argc != 2) {
412  Jim_SetResultFormatted(goi.interp,
413  "usage: %s <core_reg_num> <core_reg_value>", Jim_GetString(argv[0], NULL));
414  return JIM_ERR;
415  }
416 
417  context = current_command_context(interp);
418  assert(context);
419 
420  target = get_current_target(context);
421  if (!target) {
422  Jim_SetResultFormatted(goi.interp, "No current target");
423  return JIM_ERR;
424  }
425 
426  /* Register number */
428  if (regnum > CORE_REG_MAX_NUMBER || regnum == ARC_R61 || regnum == ARC_R62) {
429  Jim_SetResultFormatted(goi.interp, "Core register number %i "
430  "is invalid. Must less then 64 and not 61 and 62.", regnum);
431  return JIM_ERR;
432  }
433 
434  /* Register value */
436 
437  struct arc_common *arc = target_to_arc(target);
438  assert(arc);
439 
440  CHECK_RETVAL(arc_jtag_write_core_reg_one(&arc->jtag_info, regnum, value));
441 
442  return ERROR_OK;
443 }
444 
445 static const struct command_registration arc_jtag_command_group[] = {
446  {
447  .name = "get-aux-reg",
448  .jim_handler = jim_arc_get_aux_reg,
449  .mode = COMMAND_EXEC,
450  .help = "Get AUX register by number. This command does a "
451  "raw JTAG request that bypasses OpenOCD register cache "
452  "and thus is unsafe and can have unexpected consequences. "
453  "Use at your own risk.",
454  .usage = "<regnum>"
455  },
456  {
457  .name = "set-aux-reg",
458  .jim_handler = jim_arc_set_aux_reg,
459  .mode = COMMAND_EXEC,
460  .help = "Set AUX register by number. This command does a "
461  "raw JTAG request that bypasses OpenOCD register cache "
462  "and thus is unsafe and can have unexpected consequences. "
463  "Use at your own risk.",
464  .usage = "<regnum> <value>"
465  },
466  {
467  .name = "get-core-reg",
468  .jim_handler = jim_arc_get_core_reg,
469  .mode = COMMAND_EXEC,
470  .help = "Get/Set core register by number. This command does a "
471  "raw JTAG request that bypasses OpenOCD register cache "
472  "and thus is unsafe and can have unexpected consequences. "
473  "Use at your own risk.",
474  .usage = "<regnum> [<value>]"
475  },
476  {
477  .name = "set-core-reg",
478  .jim_handler = jim_arc_set_core_reg,
479  .mode = COMMAND_EXEC,
480  .help = "Get/Set core register by number. This command does a "
481  "raw JTAG request that bypasses OpenOCD register cache "
482  "and thus is unsafe and can have unexpected consequences. "
483  "Use at your own risk.",
484  .usage = "<regnum> [<value>]"
485  },
487 };
488 
489 
490 /* This function supports only bitfields. */
491 static int jim_arc_add_reg_type_struct(Jim_Interp *interp, int argc,
492  Jim_Obj * const *argv)
493 {
494  struct jim_getopt_info goi;
496 
497  LOG_DEBUG("-");
498 
499  struct command_context *ctx;
500  struct target *target;
501 
502  ctx = current_command_context(interp);
503  assert(ctx);
504  target = get_current_target(ctx);
505  if (!target) {
506  Jim_SetResultFormatted(goi.interp, "No current target");
507  return JIM_ERR;
508  }
509 
510  int e = JIM_OK;
511 
512  /* Check if the amount of arguments is not zero */
513  if (goi.argc <= 0) {
514  Jim_SetResultFormatted(goi.interp, "The command has no arguments");
515  return JIM_ERR;
516  }
517 
518  /* Estimate number of registers as (argc - 2)/4 as each -bitfield option has 3
519  * arguments while -name is required. */
520  unsigned int fields_sz = (goi.argc - 2) / 4;
521  unsigned int cur_field = 0;
522 
523  /* The maximum amount of bitfields is 32 */
524  if (fields_sz > 32) {
525  Jim_SetResultFormatted(goi.interp, "The amount of bitfields exceed 32");
526  return JIM_ERR;
527  }
528 
529  struct arc_reg_data_type *type = calloc(1, sizeof(*type));
530  struct reg_data_type_struct *struct_type = &type->data_type_struct;
531  struct reg_data_type_struct_field *fields = calloc(fields_sz, sizeof(*fields));
532  type->reg_type_struct_field = fields;
533  struct arc_reg_bitfield *bitfields = calloc(fields_sz, sizeof(*bitfields));
534  if (!(type && fields && bitfields)) {
535  Jim_SetResultFormatted(goi.interp, "Failed to allocate memory.");
536  goto fail;
537  }
538 
539  /* Initialize type */
540  type->data_type.id = type->data_type_id;
541  type->bitfields = bitfields;
542  type->data_type.type = REG_TYPE_ARCH_DEFINED;
543  type->data_type.type_class = REG_TYPE_CLASS_STRUCT;
544  type->data_type.reg_type_struct = struct_type;
545  struct_type->size = 4; /* For now ARC has only 32-bit registers */
546 
547  while (goi.argc > 0 && e == JIM_OK) {
548  struct jim_nvp *n;
550  if (e != JIM_OK) {
552  continue;
553  }
554 
555  switch (n->value) {
557  {
558  const char *name = NULL;
559  int name_len = 0;
560 
561  e = jim_arc_read_reg_name_field(&goi, &name, &name_len);
562  if (e != JIM_OK) {
563  Jim_SetResultFormatted(goi.interp, "Unable to read reg name.");
564  goto fail;
565  }
566 
567  if (name_len > REG_TYPE_MAX_NAME_LENGTH) {
568  Jim_SetResultFormatted(goi.interp, "Reg type name is too big.");
569  goto fail;
570  }
571 
572  strncpy((void *)type->data_type.id, name, name_len);
573  if (!type->data_type.id) {
574  Jim_SetResultFormatted(goi.interp, "Unable to setup reg type name.");
575  goto fail;
576  }
577 
578  break;
579  }
581  {
582  const char *field_name = NULL;
583  int field_name_len = 0;
584  e = jim_arc_read_reg_type_field(&goi, &field_name, &field_name_len, bitfields,
585  cur_field, CFG_ADD_REG_TYPE_STRUCT);
586  if (e != JIM_OK) {
587  Jim_SetResultFormatted(goi.interp, "Unable to add reg_type_struct field.");
588  goto fail;
589  }
590 
591  if (field_name_len > REG_TYPE_MAX_NAME_LENGTH) {
592  Jim_SetResultFormatted(goi.interp, "Reg type field_name_len is too big.");
593  goto fail;
594  }
595 
596  fields[cur_field].name = bitfields[cur_field].name;
597  strncpy(bitfields[cur_field].name, field_name, field_name_len);
598  if (!fields[cur_field].name) {
599  Jim_SetResultFormatted(goi.interp, "Unable to setup field name. ");
600  goto fail;
601  }
602 
603  fields[cur_field].bitfield = &(bitfields[cur_field].bitfield);
604  fields[cur_field].use_bitfields = true;
605  if (cur_field > 0)
606  fields[cur_field - 1].next = &(fields[cur_field]);
607  else
608  struct_type->fields = fields;
609 
610  cur_field += 1;
611 
612  break;
613  }
614  }
615  }
616 
617  if (!type->data_type.id) {
618  Jim_SetResultFormatted(goi.interp, "-name is a required option");
619  goto fail;
620  }
621 
623  LOG_DEBUG("added struct type {name=%s}", type->data_type.id);
624  return JIM_OK;
625 
626 fail:
627  free(type);
628  free(fields);
629  free(bitfields);
630 
631  return JIM_ERR;
632 }
633 
634 /* Add register */
643 };
644 
645 static struct jim_nvp opts_nvp_add_reg[] = {
646  { .name = "-name", .value = CFG_ADD_REG_NAME },
647  { .name = "-num", .value = CFG_ADD_REG_ARCH_NUM },
648  { .name = "-core", .value = CFG_ADD_REG_IS_CORE },
649  { .name = "-bcr", .value = CFG_ADD_REG_IS_BCR },
650  { .name = "-feature", .value = CFG_ADD_REG_GDB_FEATURE },
651  { .name = "-type", .value = CFG_ADD_REG_TYPE },
652  { .name = "-g", .value = CFG_ADD_REG_GENERAL },
653  { .name = NULL, .value = -1 }
654 };
655 
657 {
658  free(r->name);
659  free(r->gdb_xml_feature);
660  free(r);
661 }
662 
663 static int jim_arc_add_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
664 {
665  struct jim_getopt_info goi;
667 
668  struct arc_reg_desc *reg = calloc(1, sizeof(*reg));
669  if (!reg) {
670  Jim_SetResultFormatted(goi.interp, "Failed to allocate memory.");
671  return JIM_ERR;
672  }
673 
674  /* There is no architecture number that we could treat as invalid, so
675  * separate variable required to ensure that arch num has been set. */
676  bool arch_num_set = false;
677  const char *type_name = "int"; /* Default type */
678  int type_name_len = strlen(type_name);
679  int e = ERROR_OK;
680 
681  /* At least we need to specify 4 parameters: name, number and gdb_feature,
682  * which means there should be 6 arguments. Also there can be additional parameters
683  * "-type <type>", "-g" and "-core" or "-bcr" which makes maximum 10 parameters. */
684  if (goi.argc < 6 || goi.argc > 10) {
686  Jim_SetResultFormatted(goi.interp,
687  "Should be at least 6 arguments and not greater than 10: "
688  " -name <name> -num <num> -feature <gdb_feature> "
689  " [-type <type_name>] [-core|-bcr] [-g].");
690  return JIM_ERR;
691  }
692 
693  /* Parse options. */
694  while (goi.argc > 0) {
695  struct jim_nvp *n;
696  e = jim_getopt_nvp(&goi, opts_nvp_add_reg, &n);
697  if (e != JIM_OK) {
700  return e;
701  }
702 
703  switch (n->value) {
704  case CFG_ADD_REG_NAME:
705  {
706  const char *reg_name = NULL;
707  int reg_name_len = 0;
708 
709  e = jim_arc_read_reg_name_field(&goi, &reg_name, &reg_name_len);
710  if (e != JIM_OK) {
711  Jim_SetResultFormatted(goi.interp, "Unable to read register name.");
713  return e;
714  }
715 
716  reg->name = strndup(reg_name, reg_name_len);
717  break;
718  }
719  case CFG_ADD_REG_IS_CORE:
720  reg->is_core = true;
721  break;
722  case CFG_ADD_REG_IS_BCR:
723  reg->is_bcr = true;
724  break;
726  {
727  jim_wide archnum;
728 
729  if (!goi.argc) {
731  Jim_WrongNumArgs(interp, goi.argc, goi.argv, "-num <int> ...");
732  return JIM_ERR;
733  }
734 
735  e = jim_getopt_wide(&goi, &archnum);
736  if (e != JIM_OK) {
738  return e;
739  }
740 
741  reg->arch_num = archnum;
742  arch_num_set = true;
743  break;
744  }
746  {
747  const char *feature = NULL;
748  int feature_len = 0;
749 
750  e = jim_arc_read_reg_name_field(&goi, &feature, &feature_len);
751  if (e != JIM_OK) {
752  Jim_SetResultFormatted(goi.interp, "Unable to read gdb_feature.");
754  return e;
755  }
756 
757  reg->gdb_xml_feature = strndup(feature, feature_len);
758  break;
759  }
760  case CFG_ADD_REG_TYPE:
761  e = jim_arc_read_reg_name_field(&goi, &type_name, &type_name_len);
762  if (e != JIM_OK) {
763  Jim_SetResultFormatted(goi.interp, "Unable to read register type.");
765  return e;
766  }
767 
768  break;
769  case CFG_ADD_REG_GENERAL:
770  reg->is_general = true;
771  break;
772  default:
773  LOG_DEBUG("Error: Unknown parameter");
775  return JIM_ERR;
776  }
777  }
778 
779  /* Check that required fields are set */
780  const char * const errmsg = validate_register(reg, arch_num_set);
781  if (errmsg) {
782  Jim_SetResultFormatted(goi.interp, errmsg);
784  return JIM_ERR;
785  }
786 
787  /* Add new register */
788  struct command_context *ctx;
789  struct target *target;
790 
791  ctx = current_command_context(interp);
792  assert(ctx);
793  target = get_current_target(ctx);
794  if (!target) {
795  Jim_SetResultFormatted(goi.interp, "No current target");
797  return JIM_ERR;
798  }
799 
800  reg->target = target;
801 
802  e = arc_reg_add(target, reg, type_name, type_name_len);
803  if (e == ERROR_ARC_REGTYPE_NOT_FOUND) {
804  Jim_SetResultFormatted(goi.interp,
805  "Cannot find type `%s' for register `%s'.",
806  type_name, reg->name);
808  return JIM_ERR;
809  }
810 
811  return e;
812 }
813 
814 /* arc set-reg-exists ($reg_name)+
815  * Accepts any amount of register names - will set them as existing in a loop.*/
816 COMMAND_HANDLER(arc_set_reg_exists)
817 {
818  struct target * const target = get_current_target(CMD_CTX);
819  if (!target) {
820  command_print(CMD, "Unable to get current target.");
821  return JIM_ERR;
822  }
823 
824  if (!CMD_ARGC) {
825  command_print(CMD, "At least one register name must be specified.");
827  }
828 
829  for (unsigned int i = 0; i < CMD_ARGC; i++) {
830  const char * const reg_name = CMD_ARGV[i];
831  struct reg * const r = arc_reg_get_by_name(target->reg_cache, reg_name, true);
832 
833  if (!r) {
834  command_print(CMD, "Register `%s' is not found.", reg_name);
836  }
837 
838  r->exist = true;
839  }
840 
841  return JIM_OK;
842 }
843 
844 /* arc reg-field ($reg_name) ($reg_field)
845  * Reads struct type register field */
846 static int jim_arc_get_reg_field(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
847 {
848  struct jim_getopt_info goi;
849  const char *reg_name, *field_name;
850  uint32_t value;
851  int retval;
852 
854 
855  LOG_DEBUG("Reading register field");
856  if (goi.argc != 2) {
857  if (!goi.argc)
858  Jim_WrongNumArgs(interp, goi.argc, goi.argv, "<regname> <fieldname>");
859  else if (goi.argc == 1)
860  Jim_WrongNumArgs(interp, goi.argc, goi.argv, "<fieldname>");
861  else
862  Jim_WrongNumArgs(interp, goi.argc, goi.argv, "<regname> <fieldname>");
864  }
865 
866  JIM_CHECK_RETVAL(jim_getopt_string(&goi, &reg_name, NULL));
867  JIM_CHECK_RETVAL(jim_getopt_string(&goi, &field_name, NULL));
868  assert(reg_name);
869  assert(field_name);
870 
871  struct command_context * const ctx = current_command_context(interp);
872  assert(ctx);
873  struct target * const target = get_current_target(ctx);
874  if (!target) {
875  Jim_SetResultFormatted(goi.interp, "No current target");
876  return JIM_ERR;
877  }
878 
879  retval = arc_reg_get_field(target, reg_name, field_name, &value);
880 
881  switch (retval) {
882  case ERROR_OK:
883  break;
885  Jim_SetResultFormatted(goi.interp,
886  "Register `%s' has not been found.", reg_name);
889  Jim_SetResultFormatted(goi.interp,
890  "Register `%s' must have 'struct' type.", reg_name);
893  Jim_SetResultFormatted(goi.interp,
894  "Field `%s' has not been found in register `%s'.",
895  field_name, reg_name);
898  Jim_SetResultFormatted(goi.interp,
899  "Field `%s' is not a 'bitfield' field in a structure.",
900  field_name);
902  default:
903  /* Pass through other errors. */
904  return retval;
905  }
906 
907  Jim_SetResultInt(interp, value);
908 
909  return JIM_OK;
910 }
911 
912 COMMAND_HANDLER(arc_l1_cache_disable_auto_cmd)
913 {
914  bool value;
915  int retval = 0;
917  retval = CALL_COMMAND_HANDLER(handle_command_parse_bool,
918  &value, "target has caches enabled");
919  arc->has_l2cache = value;
920  arc->has_dcache = value;
921  arc->has_icache = value;
922  return retval;
923 }
924 
925 COMMAND_HANDLER(arc_l2_cache_disable_auto_cmd)
926 {
928  return CALL_COMMAND_HANDLER(handle_command_parse_bool,
929  &arc->has_l2cache, "target has l2 cache enabled");
930 }
931 
932 static int jim_handle_actionpoints_num(Jim_Interp *interp, int argc,
933  Jim_Obj * const *argv)
934 {
935  struct jim_getopt_info goi;
936  jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
937 
938  LOG_DEBUG("-");
939 
940  if (goi.argc >= 2) {
941  Jim_WrongNumArgs(interp, goi.argc, goi.argv, "[<unsigned integer>]");
942  return JIM_ERR;
943  }
944 
946  assert(context);
947 
948  struct target *target = get_current_target(context);
949 
950  if (!target) {
951  Jim_SetResultFormatted(goi.interp, "No current target");
952  return JIM_ERR;
953  }
954 
955  struct arc_common *arc = target_to_arc(target);
956  /* It is not possible to pass &arc->actionpoints_num directly to
957  * handle_command_parse_uint, because this value should be valid during
958  * "actionpoint reset, initiated by arc_set_actionpoints_num. */
959  uint32_t ap_num = arc->actionpoints_num;
960 
961  if (goi.argc == 1) {
963  int e = arc_set_actionpoints_num(target, ap_num);
964  if (e != ERROR_OK) {
965  Jim_SetResultFormatted(goi.interp,
966  "Failed to set number of actionpoints");
967  return JIM_ERR;
968  }
969  }
970 
971  Jim_SetResultInt(interp, ap_num);
972 
973  return JIM_OK;
974 }
975 
976 /* ----- Exported target commands ------------------------------------------ */
977 
978 static const struct command_registration arc_l2_cache_group_handlers[] = {
979  {
980  .name = "auto",
981  .handler = arc_l2_cache_disable_auto_cmd,
982  .mode = COMMAND_ANY,
983  .usage = "(1|0)",
984  .help = "Disable or enable L2",
985  },
987 };
988 
989 static const struct command_registration arc_cache_group_handlers[] = {
990  {
991  .name = "auto",
992  .handler = arc_l1_cache_disable_auto_cmd,
993  .mode = COMMAND_ANY,
994  .help = "Disable or enable L1",
995  .usage = "(1|0)",
996  },
997  {
998  .name = "l2",
999  .mode = COMMAND_ANY,
1000  .help = "L2 cache command group",
1001  .usage = "",
1002  .chain = arc_l2_cache_group_handlers,
1003  },
1005 };
1006 
1007 
1008 static const struct command_registration arc_core_command_handlers[] = {
1009  {
1010  .name = "add-reg-type-flags",
1011  .jim_handler = jim_arc_add_reg_type_flags,
1012  .mode = COMMAND_CONFIG,
1013  .usage = "-name <string> -flag <name> <position> "
1014  "[-flag <name> <position>]...",
1015  .help = "Add new 'flags' register data type. Only single bit flags "
1016  "are supported. Type name is global. Bitsize of register is fixed "
1017  "at 32 bits.",
1018  },
1019  {
1020  .name = "add-reg-type-struct",
1021  .jim_handler = jim_arc_add_reg_type_struct,
1022  .mode = COMMAND_CONFIG,
1023  .usage = "-name <string> -bitfield <name> <start> <end> "
1024  "[-bitfield <name> <start> <end>]...",
1025  .help = "Add new 'struct' register data type. Only bit-fields are "
1026  "supported so far, which means that for each bitfield start and end "
1027  "position bits must be specified. GDB also support type-fields, "
1028  "where common type can be used instead. Type name is global. Bitsize of "
1029  "register is fixed at 32 bits.",
1030  },
1031  {
1032  .name = "add-reg",
1033  .jim_handler = jim_arc_add_reg,
1034  .mode = COMMAND_CONFIG,
1035  .usage = "-name <string> -num <int> -feature <string> [-gdbnum <int>] "
1036  "[-core|-bcr] [-type <type_name>] [-g]",
1037  .help = "Add new register. Name, architectural number and feature name "
1038  "are required options. GDB regnum will default to previous register "
1039  "(gdbnum + 1) and shouldn't be specified in most cases. Type "
1040  "defaults to default GDB 'int'.",
1041  },
1042  {
1043  .name = "set-reg-exists",
1044  .handler = arc_set_reg_exists,
1045  .mode = COMMAND_ANY,
1046  .usage = "<register-name> [<register-name>]...",
1047  .help = "Set that register exists. Accepts multiple register names as "
1048  "arguments.",
1049  },
1050  {
1051  .name = "get-reg-field",
1052  .jim_handler = jim_arc_get_reg_field,
1053  .mode = COMMAND_ANY,
1054  .usage = "<regname> <field_name>",
1055  .help = "Returns value of field in a register with 'struct' type.",
1056  },
1057  {
1058  .name = "jtag",
1059  .mode = COMMAND_ANY,
1060  .help = "ARC JTAG specific commands",
1061  .usage = "",
1062  .chain = arc_jtag_command_group,
1063  },
1064  {
1065  .name = "cache",
1066  .mode = COMMAND_ANY,
1067  .help = "cache command group",
1068  .usage = "",
1069  .chain = arc_cache_group_handlers,
1070  },
1071  {
1072  .name = "num-actionpoints",
1073  .jim_handler = jim_handle_actionpoints_num,
1074  .mode = COMMAND_ANY,
1075  .usage = "[<unsigned integer>]",
1076  .help = "Prints or sets amount of actionpoints in the processor.",
1077  },
1079 };
1080 
1082  {
1083  .name = "arc",
1084  .mode = COMMAND_ANY,
1085  .help = "ARC monitor command group",
1086  .usage = "",
1087  .chain = arc_core_command_handlers,
1088  },
1090 };
int arc_reg_get_field(struct target *target, const char *reg_name, const char *field_name, uint32_t *value_ptr)
Definition: arc.c:542
int arc_reg_add(struct target *target, struct arc_reg_desc *arc_reg, const char *const type_name, const size_t type_name_len)
Definition: arc.c:167
void arc_reg_data_type_add(struct target *target, struct arc_reg_data_type *data_type)
Definition: arc.c:54
int arc_set_actionpoints_num(struct target *target, uint32_t ap_num)
Definition: arc.c:1723
struct reg * arc_reg_get_by_name(struct reg_cache *first, const char *name, bool search_all)
Private implementation of register_get_by_name() for ARC that doesn't skip not [yet] existing registe...
Definition: arc.c:70
static struct arc_common * target_to_arc(struct target *target)
Definition: arc.h:266
#define ERROR_ARC_REGTYPE_NOT_FOUND
Definition: arc.h:335
#define ERROR_ARC_REGISTER_FIELD_NOT_FOUND
Definition: arc.h:332
#define CORE_REG_MAX_NUMBER
Definition: arc.h:95
#define ERROR_ARC_REGISTER_IS_NOT_STRUCT
Definition: arc.h:333
#define JIM_CHECK_RETVAL(action)
Definition: arc.h:256
#define ERROR_ARC_REGISTER_NOT_FOUND
Definition: arc.h:331
#define REG_TYPE_MAX_NAME_LENGTH
Definition: arc.h:98
#define CHECK_RETVAL(action)
Definition: arc.h:246
#define ERROR_ARC_FIELD_IS_NOT_BITFIELD
Definition: arc.h:334
@ ARC_R61
Definition: arc.h:85
@ ARC_R62
Definition: arc.h:86
static int jim_arc_get_core_reg(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:358
static int jim_arc_get_reg_field(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:846
static struct jim_nvp nvp_add_reg_type_flags_opts[]
Definition: arc_cmd.c:43
static int jim_arc_read_reg_name_field(struct jim_getopt_info *goi, const char **name, int *name_len)
Definition: arc_cmd.c:67
static int jim_arc_add_reg_type_flags(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:125
add_reg_type_struct
Definition: arc_cmd.c:270
@ CFG_ADD_REG_TYPE_STRUCT_BITFIELD
Definition: arc_cmd.c:272
@ CFG_ADD_REG_TYPE_STRUCT_NAME
Definition: arc_cmd.c:271
static int jim_arc_get_aux_reg(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:321
add_reg_type_flags
Definition: arc_cmd.c:38
@ CFG_ADD_REG_TYPE_FLAGS_FLAG
Definition: arc_cmd.c:40
@ CFG_ADD_REG_TYPE_FLAGS_NAME
Definition: arc_cmd.c:39
static const struct command_registration arc_cache_group_handlers[]
Definition: arc_cmd.c:989
static int arc_cmd_jim_get_uint32(struct jim_getopt_info *goi, uint32_t *value)
Definition: arc_cmd.c:25
static int jim_handle_actionpoints_num(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:932
static int jim_arc_set_core_reg(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:401
static int jim_arc_add_reg_type_struct(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:491
opts_add_reg
Definition: arc_cmd.c:635
@ CFG_ADD_REG_ARCH_NUM
Definition: arc_cmd.c:637
@ CFG_ADD_REG_NAME
Definition: arc_cmd.c:636
@ CFG_ADD_REG_GDB_FEATURE
Definition: arc_cmd.c:640
@ CFG_ADD_REG_GENERAL
Definition: arc_cmd.c:642
@ CFG_ADD_REG_IS_BCR
Definition: arc_cmd.c:639
@ CFG_ADD_REG_IS_CORE
Definition: arc_cmd.c:638
@ CFG_ADD_REG_TYPE
Definition: arc_cmd.c:641
static const struct command_registration arc_core_command_handlers[]
Definition: arc_cmd.c:1008
static struct jim_nvp opts_nvp_add_reg[]
Definition: arc_cmd.c:645
void free_reg_desc(struct arc_reg_desc *r)
Definition: arc_cmd.c:656
static struct jim_nvp nvp_add_reg_type_struct_opts[]
Definition: arc_cmd.c:275
const struct command_registration arc_monitor_command_handlers[]
Definition: arc_cmd.c:1081
static const char * validate_register(const struct arc_reg_desc *const reg, bool arch_num_set)
Definition: arc_cmd.c:51
static int jim_arc_add_reg(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:663
static const struct command_registration arc_l2_cache_group_handlers[]
Definition: arc_cmd.c:978
static const struct command_registration arc_jtag_command_group[]
Definition: arc_cmd.c:445
static int jim_arc_read_reg_type_field(struct jim_getopt_info *goi, const char **field_name, int *field_name_len, struct arc_reg_bitfield *bitfields, int cur_field, int type)
Definition: arc_cmd.c:81
static int jim_arc_set_aux_reg(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: arc_cmd.c:281
COMMAND_HANDLER(arc_set_reg_exists)
Definition: arc_cmd.c:816
add_reg_types
Definition: arc_cmd.c:33
@ CFG_ADD_REG_TYPE_STRUCT
Definition: arc_cmd.c:35
@ CFG_ADD_REG_TYPE_FLAG
Definition: arc_cmd.c:34
int arc_jtag_read_core_reg_one(struct arc_jtag *jtag_info, uint32_t addr, uint32_t *value)
Wrapper function to ease reading of one core register.
Definition: arc_jtag.c:350
int arc_jtag_write_aux_reg_one(struct arc_jtag *jtag_info, uint32_t addr, uint32_t value)
Wrapper function to ease writing of one AUX register.
Definition: arc_jtag.c:374
int arc_jtag_write_core_reg_one(struct arc_jtag *jtag_info, uint32_t addr, uint32_t value)
Wrapper function to ease writing of one core register.
Definition: arc_jtag.c:326
int arc_jtag_read_aux_reg_one(struct arc_jtag *jtag_info, uint32_t addr, uint32_t *value)
Wrapper function to ease reading of one AUX register.
Definition: arc_jtag.c:398
const char * name
Definition: armv4_5.c:76
const char * feature
Definition: armv4_5.c:363
struct command_context * current_command_context(Jim_Interp *interp)
Definition: command.c:187
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:117
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:145
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:387
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint8_t type
Definition: esp_usb_jtag.c:0
int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
Remove argv[0] as wide.
Definition: jim-nvp.c:221
int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
GetOpt - how to.
Definition: jim-nvp.c:148
int jim_getopt_string(struct jim_getopt_info *goi, const char **puthere, int *len)
Remove argv[0] as string.
Definition: jim-nvp.c:187
int jim_getopt_nvp(struct jim_getopt_info *goi, const struct jim_nvp *nvp, struct jim_nvp **puthere)
Remove argv[0] as NVP.
Definition: jim-nvp.c:236
void jim_getopt_nvp_unknown(struct jim_getopt_info *goi, const struct jim_nvp *nvptable, int hadprefix)
Create an appropriate error message for an NVP.
Definition: jim-nvp.c:252
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
@ REG_TYPE_INT
Definition: register.h:21
@ REG_TYPE_ARCH_DEFINED
Definition: register.h:38
@ REG_TYPE_CLASS_FLAGS
Definition: register.h:96
@ REG_TYPE_CLASS_STRUCT
Definition: register.h:95
char * strndup(const char *s, size_t n)
Definition: replacements.c:111
struct target * target
Definition: rtt/rtt.c:26
bool has_icache
Definition: arc.h:195
bool has_l2cache
Definition: arc.h:196
unsigned int actionpoints_num
Definition: arc.h:240
bool has_dcache
Definition: arc.h:194
struct arc_jtag jtag_info
Definition: arc.h:188
char name[REG_TYPE_MAX_NAME_LENGTH]
Definition: arc.h:136
struct reg_data_type_bitfield bitfield
Definition: arc.h:135
char * gdb_xml_feature
Definition: arc.h:310
char * name
Definition: arc.h:301
Jim_Interp * interp
Definition: command.h:53
const char * name
Definition: command.h:229
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:135
Jim_Interp * interp
Definition: jim-nvp.h:136
Jim_Obj *const * argv
Definition: jim-nvp.h:138
Name Value Pairs, aka: NVP.
Definition: jim-nvp.h:59
const char * name
Definition: jim-nvp.h:60
int value
Definition: jim-nvp.h:61
enum reg_type type
Definition: register.h:63
struct reg_data_type_flags_field * next
Definition: register.h:84
struct reg_data_type_bitfield * bitfield
Definition: register.h:83
struct reg_data_type_flags_field * fields
Definition: register.h:89
struct reg_data_type_bitfield * bitfield
Definition: register.h:70
struct reg_data_type_struct_field * next
Definition: register.h:73
struct reg_data_type_struct_field * fields
Definition: register.h:78
Definition: register.h:111
bool exist
Definition: register.h:128
const char * name
Definition: register.h:113
Definition: target.h:120
struct reg_cache * reg_cache
Definition: target.h:163
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:536
#define NULL
Definition: usb.h:16