Introduce ARCv2 architecture related code
[openocd.git] / src / target / arc_cmd.c
1 /***************************************************************************
2 * Copyright (C) 2013-2015,2019-2020 Synopsys, Inc. *
3 * Frank Dols <frank.dols@synopsys.com> *
4 * Mischa Jonker <mischa.jonker@synopsys.com> *
5 * Anton Kolesov <anton.kolesov@synopsys.com> *
6 * Evgeniy Didin <didin@synopsys.com> *
7 * *
8 * SPDX-License-Identifier: GPL-2.0-or-later *
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(Jim_GetOptInfo *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
33 enum add_reg_types {
34 CFG_ADD_REG_TYPE_FLAG,
35 CFG_ADD_REG_TYPE_STRUCT,
36 };
37 /* Add flags register data type */
38 enum add_reg_type_flags {
39 CFG_ADD_REG_TYPE_FLAGS_NAME,
40 CFG_ADD_REG_TYPE_FLAGS_FLAG,
41 };
42
43 static 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 */
67 static int jim_arc_read_reg_name_field(Jim_GetOptInfo *goi,
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(Jim_GetOptInfo *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 argmunets 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 argnuments 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 Jim_GetOptInfo goi;
129 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
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 argnuments is not zero */
147 if (goi.argc <= 0) {
148 Jim_SetResultFormatted(goi.interp, "The command has no argnuments");
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 /* Tha maximum amount of bitfilds 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 struct arc_reg_bitfield *bitfields = calloc(fields_sz, sizeof(*type));
167 if (!(type && fields && bitfields)) {
168 Jim_SetResultFormatted(goi.interp, "Failed to allocate memory.");
169 goto fail;
170 }
171
172 /* Initialize type */
173 type->bitfields = bitfields;
174 type->data_type.id = type->data_type_id;
175 type->data_type.type = REG_TYPE_ARCH_DEFINED;
176 type->data_type.type_class = REG_TYPE_CLASS_FLAGS;
177 type->data_type.reg_type_flags = flags;
178 flags->size = 4; /* For now ARC has only 32-bit registers */
179
180 while (goi.argc > 0 && e == JIM_OK) {
181 Jim_Nvp *n;
182 e = Jim_GetOpt_Nvp(&goi, nvp_add_reg_type_flags_opts, &n);
183 if (e != JIM_OK) {
184 Jim_GetOpt_NvpUnknown(&goi, nvp_add_reg_type_flags_opts, 0);
185 continue;
186 }
187
188 switch (n->value) {
189 case CFG_ADD_REG_TYPE_FLAGS_NAME:
190 {
191 const char *name = NULL;
192 int name_len = 0;
193
194 e = jim_arc_read_reg_name_field(&goi, &name, &name_len);
195 if (e != JIM_OK) {
196 Jim_SetResultFormatted(goi.interp, "Unable to read reg name.");
197 goto fail;
198 }
199
200 if (name_len > REG_TYPE_MAX_NAME_LENGTH) {
201 Jim_SetResultFormatted(goi.interp, "Reg type name is too big.");
202 goto fail;
203 }
204
205 strncpy((void *)type->data_type.id, name, name_len);
206 if (!type->data_type.id) {
207 Jim_SetResultFormatted(goi.interp, "Unable to setup reg type name.");
208 goto fail;
209 }
210
211 break;
212 }
213
214 case CFG_ADD_REG_TYPE_FLAGS_FLAG:
215 {
216 const char *field_name = NULL;
217 int field_name_len = 0;
218
219 e = jim_arc_read_reg_type_field(&goi, &field_name, &field_name_len, bitfields,
220 cur_field, CFG_ADD_REG_TYPE_FLAG);
221 if (e != JIM_OK) {
222 Jim_SetResultFormatted(goi.interp, "Unable to add reg_type_flag field.");
223 goto fail;
224 }
225
226 if (field_name_len > REG_TYPE_MAX_NAME_LENGTH) {
227 Jim_SetResultFormatted(goi.interp, "Reg type field_name_len is too big.");
228 goto fail;
229 }
230
231 fields[cur_field].name = bitfields[cur_field].name;
232 strncpy(bitfields[cur_field].name, field_name, field_name_len);
233 if (!fields[cur_field].name) {
234 Jim_SetResultFormatted(goi.interp, "Unable to setup field name. ");
235 goto fail;
236 }
237
238 fields[cur_field].bitfield = &(bitfields[cur_field].bitfield);
239 if (cur_field > 0)
240 fields[cur_field - 1].next = &(fields[cur_field]);
241 else
242 flags->fields = fields;
243
244 cur_field += 1;
245 break;
246 }
247 }
248 }
249
250 if (!type->data_type.id) {
251 Jim_SetResultFormatted(goi.interp, "-name is a required option");
252 goto fail;
253 }
254
255 arc_reg_data_type_add(target, type);
256
257 LOG_DEBUG("added flags type {name=%s}", type->data_type.id);
258
259 return JIM_OK;
260 fail:
261 free(type);
262 free(fields);
263 free(bitfields);
264
265 return JIM_ERR;
266 }
267
268 /* Add struct register data type */
269 enum add_reg_type_struct {
270 CFG_ADD_REG_TYPE_STRUCT_NAME,
271 CFG_ADD_REG_TYPE_STRUCT_BITFIELD,
272 };
273
274 static Jim_Nvp nvp_add_reg_type_struct_opts[] = {
275 { .name = "-name", .value = CFG_ADD_REG_TYPE_STRUCT_NAME },
276 { .name = "-bitfield", .value = CFG_ADD_REG_TYPE_STRUCT_BITFIELD },
277 { .name = NULL, .value = -1 }
278 };
279
280 static int jim_arc_set_aux_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
281 {
282
283 struct command_context *context;
284 struct target *target;
285 uint32_t regnum;
286 uint32_t value;
287
288 Jim_GetOptInfo goi;
289 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
290
291 if (goi.argc != 2) {
292 Jim_SetResultFormatted(goi.interp,
293 "usage: %s <aux_reg_num> <aux_reg_value>", Jim_GetString(argv[0], NULL));
294 return JIM_ERR;
295 }
296
297 context = current_command_context(interp);
298 assert(context);
299
300 target = get_current_target(context);
301 if (!target) {
302 Jim_SetResultFormatted(goi.interp, "No current target");
303 return JIM_ERR;
304 }
305
306 /* Register number */
307 JIM_CHECK_RETVAL(arc_cmd_jim_get_uint32(&goi, &regnum));
308
309 /* Register value */
310 JIM_CHECK_RETVAL(arc_cmd_jim_get_uint32(&goi, &value));
311
312 struct arc_common *arc = target_to_arc(target);
313 assert(arc);
314
315 CHECK_RETVAL(arc_jtag_write_aux_reg_one(&arc->jtag_info, regnum, value));
316
317 return ERROR_OK;
318 }
319
320 static int jim_arc_get_aux_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
321 {
322 struct command_context *context;
323 struct target *target;
324 uint32_t regnum;
325 uint32_t value;
326
327 Jim_GetOptInfo goi;
328 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
329
330 if (goi.argc != 1) {
331 Jim_SetResultFormatted(goi.interp,
332 "usage: %s <aux_reg_num>", Jim_GetString(argv[0], NULL));
333 return JIM_ERR;
334 }
335
336 context = current_command_context(interp);
337 assert(context);
338
339 target = get_current_target(context);
340 if (!target) {
341 Jim_SetResultFormatted(goi.interp, "No current target");
342 return JIM_ERR;
343 }
344
345 /* Register number */
346 JIM_CHECK_RETVAL(arc_cmd_jim_get_uint32(&goi, &regnum));
347
348 struct arc_common *arc = target_to_arc(target);
349 assert(arc);
350
351 CHECK_RETVAL(arc_jtag_read_aux_reg_one(&arc->jtag_info, regnum, &value));
352 Jim_SetResultInt(interp, value);
353
354 return ERROR_OK;
355 }
356
357 static int jim_arc_get_core_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
358 {
359 struct command_context *context;
360 struct target *target;
361 uint32_t regnum;
362 uint32_t value;
363
364 Jim_GetOptInfo goi;
365 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
366
367 if (goi.argc != 1) {
368 Jim_SetResultFormatted(goi.interp,
369 "usage: %s <core_reg_num>", Jim_GetString(argv[0], NULL));
370 return JIM_ERR;
371 }
372
373 context = current_command_context(interp);
374 assert(context);
375
376 target = get_current_target(context);
377 if (!target) {
378 Jim_SetResultFormatted(goi.interp, "No current target");
379 return JIM_ERR;
380 }
381
382 /* Register number */
383 JIM_CHECK_RETVAL(arc_cmd_jim_get_uint32(&goi, &regnum));
384 if (regnum > CORE_REG_MAX_NUMBER || regnum == CORE_R61_NUM || regnum == CORE_R62_NUM) {
385 Jim_SetResultFormatted(goi.interp, "Core register number %i " \
386 "is invalid. Must less then 64 and not 61 and 62.", regnum);
387 return JIM_ERR;
388 }
389
390 struct arc_common *arc = target_to_arc(target);
391 assert(arc);
392
393 /* Read value */
394 CHECK_RETVAL(arc_jtag_read_core_reg_one(&arc->jtag_info, regnum, &value));
395 Jim_SetResultInt(interp, value);
396
397 return ERROR_OK;
398 }
399
400 static int jim_arc_set_core_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
401 {
402 struct command_context *context;
403 struct target *target;
404 uint32_t regnum;
405 uint32_t value;
406
407 Jim_GetOptInfo goi;
408 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
409
410 if (goi.argc != 2) {
411 Jim_SetResultFormatted(goi.interp,
412 "usage: %s <core_reg_num> <core_reg_value>", Jim_GetString(argv[0], NULL));
413 return JIM_ERR;
414 }
415
416 context = current_command_context(interp);
417 assert(context);
418
419 target = get_current_target(context);
420 if (!target) {
421 Jim_SetResultFormatted(goi.interp, "No current target");
422 return JIM_ERR;
423 }
424
425 /* Register number */
426 JIM_CHECK_RETVAL(arc_cmd_jim_get_uint32(&goi, &regnum));
427 if (regnum > CORE_REG_MAX_NUMBER || regnum == CORE_R61_NUM || regnum == CORE_R62_NUM) {
428 Jim_SetResultFormatted(goi.interp, "Core register number %i " \
429 "is invalid. Must less then 64 and not 61 and 62.", regnum);
430 return JIM_ERR;
431 }
432
433 /* Register value */
434 JIM_CHECK_RETVAL(arc_cmd_jim_get_uint32(&goi, &value));
435
436 struct arc_common *arc = target_to_arc(target);
437 assert(arc);
438
439 CHECK_RETVAL(arc_jtag_write_core_reg_one(&arc->jtag_info, regnum, value));
440
441 return ERROR_OK;
442 }
443
444 static const struct command_registration arc_jtag_command_group[] = {
445 {
446 .name = "get-aux-reg",
447 .jim_handler = jim_arc_get_aux_reg,
448 .mode = COMMAND_EXEC,
449 .help = "Get AUX register by number. This command does a " \
450 "raw JTAG request that bypasses OpenOCD register cache "\
451 "and thus is unsafe and can have unexpected consequences. "\
452 "Use at your own risk.",
453 .usage = "arc jtag get-aux-reg <regnum>"
454 },
455 {
456 .name = "set-aux-reg",
457 .jim_handler = jim_arc_set_aux_reg,
458 .mode = COMMAND_EXEC,
459 .help = "Set AUX register by number. This command does a " \
460 "raw JTAG request that bypasses OpenOCD register cache "\
461 "and thus is unsafe and can have unexpected consequences. "\
462 "Use at your own risk.",
463 .usage = "arc jtag set-aux-reg <regnum> <value>"
464 },
465 {
466 .name = "get-core-reg",
467 .jim_handler = jim_arc_get_core_reg,
468 .mode = COMMAND_EXEC,
469 .help = "Get/Set core register by number. This command does a " \
470 "raw JTAG request that bypasses OpenOCD register cache "\
471 "and thus is unsafe and can have unexpected consequences. "\
472 "Use at your own risk.",
473 .usage = "arc jtag get-core-reg <regnum> [<value>]"
474 },
475 {
476 .name = "set-core-reg",
477 .jim_handler = jim_arc_set_core_reg,
478 .mode = COMMAND_EXEC,
479 .help = "Get/Set core register by number. This command does a " \
480 "raw JTAG request that bypasses OpenOCD register cache "\
481 "and thus is unsafe and can have unexpected consequences. "\
482 "Use at your own risk.",
483 .usage = "arc jtag set-core-reg <regnum> [<value>]"
484 },
485 COMMAND_REGISTRATION_DONE
486 };
487
488
489 /* This function supports only bitfields. */
490 static int jim_arc_add_reg_type_struct(Jim_Interp *interp, int argc,
491 Jim_Obj * const *argv)
492 {
493 Jim_GetOptInfo goi;
494 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
495
496 LOG_DEBUG("-");
497
498 struct command_context *ctx;
499 struct target *target;
500
501 ctx = current_command_context(interp);
502 assert(ctx);
503 target = get_current_target(ctx);
504 if (!target) {
505 Jim_SetResultFormatted(goi.interp, "No current target");
506 return JIM_ERR;
507 }
508
509 int e = JIM_OK;
510
511 /* Check if the amount of argnuments is not zero */
512 if (goi.argc <= 0) {
513 Jim_SetResultFormatted(goi.interp, "The command has no argnuments");
514 return JIM_ERR;
515 }
516
517 /* Estimate number of registers as (argc - 2)/4 as each -bitfield option has 3
518 * arguments while -name is required. */
519 unsigned int fields_sz = (goi.argc - 2) / 4;
520 unsigned int cur_field = 0;
521
522 /* Tha maximum amount of bitfilds is 32 */
523 if (fields_sz > 32) {
524 Jim_SetResultFormatted(goi.interp, "The amount of bitfields exceed 32");
525 return JIM_ERR;
526 }
527
528 struct arc_reg_data_type *type = calloc(1, sizeof(*type));
529 struct reg_data_type_struct *struct_type = &type->data_type_struct;
530 struct reg_data_type_struct_field *fields = calloc(fields_sz, sizeof(*fields));
531 struct arc_reg_bitfield *bitfields = calloc(fields_sz, sizeof(*type));
532 if (!(type && fields && bitfields)) {
533 Jim_SetResultFormatted(goi.interp, "Failed to allocate memory.");
534 goto fail;
535 }
536
537 /* Initialize type */
538 type->data_type.id = type->data_type_id;
539 type->bitfields = bitfields;
540 type->data_type.type = REG_TYPE_ARCH_DEFINED;
541 type->data_type.type_class = REG_TYPE_CLASS_STRUCT;
542 type->data_type.reg_type_struct = struct_type;
543 struct_type->size = 4; /* For now ARC has only 32-bit registers */
544
545 while (goi.argc > 0 && e == JIM_OK) {
546 Jim_Nvp *n;
547 e = Jim_GetOpt_Nvp(&goi, nvp_add_reg_type_struct_opts, &n);
548 if (e != JIM_OK) {
549 Jim_GetOpt_NvpUnknown(&goi, nvp_add_reg_type_struct_opts, 0);
550 continue;
551 }
552
553 switch (n->value) {
554 case CFG_ADD_REG_TYPE_STRUCT_NAME:
555 {
556 const char *name = NULL;
557 int name_len = 0;
558
559 e = jim_arc_read_reg_name_field(&goi, &name, &name_len);
560 if (e != JIM_OK) {
561 Jim_SetResultFormatted(goi.interp, "Unable to read reg name.");
562 goto fail;
563 }
564
565 if (name_len > REG_TYPE_MAX_NAME_LENGTH) {
566 Jim_SetResultFormatted(goi.interp, "Reg type name is too big.");
567 goto fail;
568 }
569
570 strncpy((void *)type->data_type.id, name, name_len);
571 if (!type->data_type.id) {
572 Jim_SetResultFormatted(goi.interp, "Unable to setup reg type name.");
573 goto fail;
574 }
575
576 break;
577 }
578 case CFG_ADD_REG_TYPE_STRUCT_BITFIELD:
579 {
580 const char *field_name = NULL;
581 int field_name_len = 0;
582 e = jim_arc_read_reg_type_field(&goi, &field_name, &field_name_len, bitfields,
583 cur_field, CFG_ADD_REG_TYPE_STRUCT);
584 if (e != JIM_OK) {
585 Jim_SetResultFormatted(goi.interp, "Unable to add reg_type_struct field.");
586 goto fail;
587 }
588
589 if (field_name_len > REG_TYPE_MAX_NAME_LENGTH) {
590 Jim_SetResultFormatted(goi.interp, "Reg type field_name_len is too big.");
591 goto fail;
592 }
593
594 fields[cur_field].name = bitfields[cur_field].name;
595 strncpy(bitfields[cur_field].name, field_name, field_name_len);
596 if (!fields[cur_field].name) {
597 Jim_SetResultFormatted(goi.interp, "Unable to setup field name. ");
598 goto fail;
599 }
600
601 fields[cur_field].bitfield = &(bitfields[cur_field].bitfield);
602 fields[cur_field].use_bitfields = true;
603 if (cur_field > 0)
604 fields[cur_field - 1].next = &(fields[cur_field]);
605 else
606 struct_type->fields = fields;
607
608 cur_field += 1;
609
610 break;
611 }
612 }
613 }
614
615 if (!type->data_type.id) {
616 Jim_SetResultFormatted(goi.interp, "-name is a required option");
617 goto fail;
618 }
619
620 arc_reg_data_type_add(target, type);
621 LOG_DEBUG("added struct type {name=%s}", type->data_type.id);
622 return JIM_OK;
623
624 fail:
625 free(type);
626 free(fields);
627 free(bitfields);
628
629 return JIM_ERR;
630 }
631
632 /* Add register */
633 enum opts_add_reg {
634 CFG_ADD_REG_NAME,
635 CFG_ADD_REG_ARCH_NUM,
636 CFG_ADD_REG_IS_CORE,
637 CFG_ADD_REG_IS_BCR,
638 CFG_ADD_REG_GDB_FEATURE,
639 CFG_ADD_REG_TYPE,
640 CFG_ADD_REG_GENERAL,
641 };
642
643 static Jim_Nvp opts_nvp_add_reg[] = {
644 { .name = "-name", .value = CFG_ADD_REG_NAME },
645 { .name = "-num", .value = CFG_ADD_REG_ARCH_NUM },
646 { .name = "-core", .value = CFG_ADD_REG_IS_CORE },
647 { .name = "-bcr", .value = CFG_ADD_REG_IS_BCR },
648 { .name = "-feature", .value = CFG_ADD_REG_GDB_FEATURE },
649 { .name = "-type", .value = CFG_ADD_REG_TYPE },
650 { .name = "-g", .value = CFG_ADD_REG_GENERAL },
651 { .name = NULL, .value = -1 }
652 };
653
654 void free_reg_desc(struct arc_reg_desc *r)
655 {
656 free(r->name);
657 free(r->gdb_xml_feature);
658 free(r);
659 }
660
661 static int jim_arc_add_reg(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
662 {
663 Jim_GetOptInfo goi;
664 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
665
666 struct arc_reg_desc *reg = calloc(1, sizeof(*reg));
667 if (!reg) {
668 Jim_SetResultFormatted(goi.interp, "Failed to allocate memory.");
669 return JIM_ERR;
670 }
671
672 /* There is no architecture number that we could treat as invalid, so
673 * separate variable requried to ensure that arch num has been set. */
674 bool arch_num_set = false;
675 const char *type_name = "int"; /* Default type */
676 int type_name_len = strlen(type_name);
677 int e = ERROR_OK;
678
679 /* At least we need to specify 4 parameters: name, number, type and gdb_feature,
680 * which means there should be 8 arguments */
681 if (goi.argc < 8) {
682 free_reg_desc(reg);
683 Jim_SetResultFormatted(goi.interp,
684 "Should be at least 8 argnuments: -name <name> "
685 "-num <num> -type <type> -feature <gdb_feature>.");
686 return JIM_ERR;
687 }
688
689 /* Parse options. */
690 while (goi.argc > 0) {
691 Jim_Nvp *n;
692 e = Jim_GetOpt_Nvp(&goi, opts_nvp_add_reg, &n);
693 if (e != JIM_OK) {
694 Jim_GetOpt_NvpUnknown(&goi, opts_nvp_add_reg, 0);
695 free_reg_desc(reg);
696 return e;
697 }
698
699 switch (n->value) {
700 case CFG_ADD_REG_NAME:
701 {
702 const char *reg_name = NULL;
703 int reg_name_len = 0;
704
705 e = jim_arc_read_reg_name_field(&goi, &reg_name, &reg_name_len);
706 if (e != JIM_OK) {
707 Jim_SetResultFormatted(goi.interp, "Unable to read register name.");
708 free_reg_desc(reg);
709 return e;
710 }
711
712 reg->name = strndup(reg_name, reg_name_len);
713 break;
714 }
715 case CFG_ADD_REG_IS_CORE:
716 reg->is_core = true;
717 break;
718 case CFG_ADD_REG_IS_BCR:
719 reg->is_bcr = true;
720 break;
721 case CFG_ADD_REG_ARCH_NUM:
722 {
723 jim_wide archnum;
724
725 if (!goi.argc) {
726 free_reg_desc(reg);
727 Jim_WrongNumArgs(interp, goi.argc, goi.argv, "-num <int> ...");
728 return JIM_ERR;
729 }
730
731 e = Jim_GetOpt_Wide(&goi, &archnum);
732 if (e != JIM_OK) {
733 free_reg_desc(reg);
734 return e;
735 }
736
737 reg->arch_num = archnum;
738 arch_num_set = true;
739 break;
740 }
741 case CFG_ADD_REG_GDB_FEATURE:
742 {
743 const char *feature = NULL;
744 int feature_len = 0;
745
746 e = jim_arc_read_reg_name_field(&goi, &feature, &feature_len);
747 if (e != JIM_OK) {
748 Jim_SetResultFormatted(goi.interp, "Unable to read gdb_feature.");
749 free_reg_desc(reg);
750 return e;
751 }
752
753 reg->gdb_xml_feature = strndup(feature, feature_len);
754 break;
755 }
756 case CFG_ADD_REG_TYPE:
757 e = jim_arc_read_reg_name_field(&goi, &type_name, &type_name_len);
758 if (e != JIM_OK) {
759 Jim_SetResultFormatted(goi.interp, "Unable to read register type.");
760 free_reg_desc(reg);
761 return e;
762 }
763
764 break;
765 case CFG_ADD_REG_GENERAL:
766 reg->is_general = true;
767 break;
768 default:
769 LOG_DEBUG("Error: Unknown parameter");
770 free_reg_desc(reg);
771 return JIM_ERR;
772 }
773 }
774
775 /* Check that required fields are set */
776 const char * const errmsg = validate_register(reg, arch_num_set);
777 if (errmsg) {
778 Jim_SetResultFormatted(goi.interp, errmsg);
779 free_reg_desc(reg);
780 return JIM_ERR;
781 }
782
783 /* Add new register */
784 struct command_context *ctx;
785 struct target *target;
786
787 ctx = current_command_context(interp);
788 assert(ctx);
789 target = get_current_target(ctx);
790 if (!target) {
791 Jim_SetResultFormatted(goi.interp, "No current target");
792 return JIM_ERR;
793 }
794
795 reg->target = target;
796
797 e = arc_reg_add(target, reg, type_name, type_name_len);
798 if (e == ERROR_ARC_REGTYPE_NOT_FOUND) {
799 Jim_SetResultFormatted(goi.interp,
800 "Cannot find type `%s' for register `%s'.",
801 type_name, reg->name);
802 free_reg_desc(reg);
803 return JIM_ERR;
804 }
805
806 return e;
807 }
808
809 /* arc set-reg-exists ($reg_name)+
810 * Accepts any amount of register names - will set them as existing in a loop.*/
811 COMMAND_HANDLER(arc_set_reg_exists)
812 {
813 struct target * const target = get_current_target(CMD_CTX);
814 if (!target) {
815 command_print(CMD, "Unable to get current target.");
816 return JIM_ERR;
817 }
818
819 if (!CMD_ARGC) {
820 command_print(CMD, "At least one register name must be specified.");
821 return ERROR_COMMAND_SYNTAX_ERROR;
822 }
823
824 for (unsigned int i = 0; i < CMD_ARGC; i++) {
825 const char * const reg_name = CMD_ARGV[i];
826 struct reg * const r = arc_reg_get_by_name(target->reg_cache, reg_name, true);
827
828 if (!r) {
829 command_print(CMD, "Register `%s' is not found.", reg_name);
830 return ERROR_COMMAND_ARGUMENT_INVALID;
831 }
832
833 r->exist = true;
834 }
835
836 return JIM_OK;
837 }
838
839 /* arc reg-field ($reg_name) ($reg_field)
840 * Reads struct type register field */
841 static int jim_arc_get_reg_field(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
842 {
843 Jim_GetOptInfo goi;
844 const char *reg_name, *field_name;
845 uint32_t value;
846 int retval;
847
848 JIM_CHECK_RETVAL(Jim_GetOpt_Setup(&goi, interp, argc-1, argv+1));
849
850 LOG_DEBUG("Reading register field");
851 if (goi.argc != 2) {
852 if (!goi.argc)
853 Jim_WrongNumArgs(interp, goi.argc, goi.argv, "<regname> <fieldname>");
854 else if (goi.argc == 1)
855 Jim_WrongNumArgs(interp, goi.argc, goi.argv, "<fieldname>");
856 else
857 Jim_WrongNumArgs(interp, goi.argc, goi.argv, "<regname> <fieldname>");
858 return ERROR_COMMAND_SYNTAX_ERROR;
859 }
860
861 JIM_CHECK_RETVAL(Jim_GetOpt_String(&goi, &reg_name, NULL));
862 JIM_CHECK_RETVAL(Jim_GetOpt_String(&goi, &field_name, NULL));
863 assert(reg_name);
864 assert(field_name);
865
866 struct command_context * const ctx = current_command_context(interp);
867 assert(ctx);
868 struct target * const target = get_current_target(ctx);
869 if (!target) {
870 Jim_SetResultFormatted(goi.interp, "No current target");
871 return JIM_ERR;
872 }
873
874 retval = arc_reg_get_field(target, reg_name, field_name, &value);
875
876 switch (retval) {
877 case ERROR_OK:
878 break;
879 case ERROR_ARC_REGISTER_NOT_FOUND:
880 Jim_SetResultFormatted(goi.interp,
881 "Register `%s' has not been found.", reg_name);
882 return ERROR_COMMAND_ARGUMENT_INVALID;
883 case ERROR_ARC_REGISTER_IS_NOT_STRUCT:
884 Jim_SetResultFormatted(goi.interp,
885 "Register `%s' must have 'struct' type.", reg_name);
886 return ERROR_COMMAND_ARGUMENT_INVALID;
887 case ERROR_ARC_REGISTER_FIELD_NOT_FOUND:
888 Jim_SetResultFormatted(goi.interp,
889 "Field `%s' has not been found in register `%s'.",
890 field_name, reg_name);
891 return ERROR_COMMAND_ARGUMENT_INVALID;
892 case ERROR_ARC_FIELD_IS_NOT_BITFIELD:
893 Jim_SetResultFormatted(goi.interp,
894 "Field `%s' is not a 'bitfield' field in a structure.",
895 field_name);
896 return ERROR_COMMAND_ARGUMENT_INVALID;
897 default:
898 /* Pass through other errors. */
899 return retval;
900 }
901
902 Jim_SetResultInt(interp, value);
903
904 return JIM_OK;
905 }
906
907 /* ----- Exported target commands ------------------------------------------ */
908
909 static const struct command_registration arc_core_command_handlers[] = {
910 {
911 .name = "add-reg-type-flags",
912 .jim_handler = jim_arc_add_reg_type_flags,
913 .mode = COMMAND_CONFIG,
914 .usage = "arc ardd-reg-type-flags -name <string> -flag <name> <position> "
915 "[-flag <name> <position>]...",
916 .help = "Add new 'flags' register data type. Only single bit flags "
917 "are supported. Type name is global. Bitsize of register is fixed "
918 "at 32 bits.",
919 },
920 {
921 .name = "add-reg-type-struct",
922 .jim_handler = jim_arc_add_reg_type_struct,
923 .mode = COMMAND_CONFIG,
924 .usage = "arc add-reg-type-struct -name <string> -bitfield <name> <start> <end> "
925 "[-bitfield <name> <start> <end>]...",
926 .help = "Add new 'struct' register data type. Only bit-fields are "
927 "supported so far, which means that for each bitfield start and end "
928 "position bits must be specified. GDB also support type-fields, "
929 "where common type can be used instead. Type name is global. Bitsize of "
930 "register is fixed at 32 bits.",
931 },
932 {
933 .name = "add-reg",
934 .jim_handler = jim_arc_add_reg,
935 .mode = COMMAND_CONFIG,
936 .usage = "arc add-reg -name <string> -num <int> -feature <string> [-gdbnum <int>] "
937 "[-core|-bcr] [-type <type_name>] [-g]",
938 .help = "Add new register. Name, architectural number and feature name "
939 "are requried options. GDB regnum will default to previous register "
940 "(gdbnum + 1) and shouldn't be specified in most cases. Type "
941 "defaults to default GDB 'int'.",
942 },
943 {
944 .name = "set-reg-exists",
945 .handler = arc_set_reg_exists,
946 .mode = COMMAND_ANY,
947 .usage = "arc set-reg-exists <register-name> [<register-name>]...",
948 .help = "Set that register exists. Accepts multiple register names as "
949 "arguments.",
950 },
951 {
952 .name = "get-reg-field",
953 .jim_handler = jim_arc_get_reg_field,
954 .mode = COMMAND_ANY,
955 .usage = "arc get-reg-field <regname> <field_name>",
956 .help = "Returns value of field in a register with 'struct' type.",
957 },
958 {
959 .name = "jtag",
960 .mode = COMMAND_ANY,
961 .help = "ARC JTAG specific commands",
962 .usage = "",
963 .chain = arc_jtag_command_group,
964 },
965 COMMAND_REGISTRATION_DONE
966 };
967
968 const struct command_registration arc_monitor_command_handlers[] = {
969 {
970 .name = "arc",
971 .mode = COMMAND_ANY,
972 .help = "ARC monitor command group",
973 .usage = "Help info ...",
974 .chain = arc_core_command_handlers,
975 },
976 COMMAND_REGISTRATION_DONE
977 };