OpenOCD
riscv_reg.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #include "gdb_regs.h"
8 #include "riscv.h"
9 #include "riscv_reg.h"
10 #include "riscv_reg_impl.h"
20 #include "debug_defines.h"
21 #include "riscv-011.h"
22 #include "riscv-013.h"
23 #include "field_helpers.h"
24 
25 static const char * const default_reg_names[GDB_REGNO_COUNT] = {
26  [GDB_REGNO_ZERO] = "zero",
27  [GDB_REGNO_RA] = "ra",
28  [GDB_REGNO_SP] = "sp",
29  [GDB_REGNO_GP] = "gp",
30  [GDB_REGNO_TP] = "tp",
31  [GDB_REGNO_T0] = "t0",
32  [GDB_REGNO_T1] = "t1",
33  [GDB_REGNO_T2] = "t2",
34  [GDB_REGNO_FP] = "fp",
35  [GDB_REGNO_S1] = "s1",
36  [GDB_REGNO_A0] = "a0",
37  [GDB_REGNO_A1] = "a1",
38  [GDB_REGNO_A2] = "a2",
39  [GDB_REGNO_A3] = "a3",
40  [GDB_REGNO_A4] = "a4",
41  [GDB_REGNO_A5] = "a5",
42  [GDB_REGNO_A6] = "a6",
43  [GDB_REGNO_A7] = "a7",
44  [GDB_REGNO_S2] = "s2",
45  [GDB_REGNO_S3] = "s3",
46  [GDB_REGNO_S4] = "s4",
47  [GDB_REGNO_S5] = "s5",
48  [GDB_REGNO_S6] = "s6",
49  [GDB_REGNO_S7] = "s7",
50  [GDB_REGNO_S8] = "s8",
51  [GDB_REGNO_S9] = "s9",
52  [GDB_REGNO_S10] = "s10",
53  [GDB_REGNO_S11] = "s11",
54  [GDB_REGNO_T3] = "t3",
55  [GDB_REGNO_T4] = "t4",
56  [GDB_REGNO_T5] = "t5",
57  [GDB_REGNO_T6] = "t6",
58  [GDB_REGNO_PC] = "pc",
59  [GDB_REGNO_PRIV] = "priv",
60  [GDB_REGNO_FT0] = "ft0",
61  [GDB_REGNO_FT1] = "ft1",
62  [GDB_REGNO_FT2] = "ft2",
63  [GDB_REGNO_FT3] = "ft3",
64  [GDB_REGNO_FT4] = "ft4",
65  [GDB_REGNO_FT5] = "ft5",
66  [GDB_REGNO_FT6] = "ft6",
67  [GDB_REGNO_FT7] = "ft7",
68  [GDB_REGNO_FS0] = "fs0",
69  [GDB_REGNO_FS1] = "fs1",
70  [GDB_REGNO_FA0] = "fa0",
71  [GDB_REGNO_FA1] = "fa1",
72  [GDB_REGNO_FA2] = "fa2",
73  [GDB_REGNO_FA3] = "fa3",
74  [GDB_REGNO_FA4] = "fa4",
75  [GDB_REGNO_FA5] = "fa5",
76  [GDB_REGNO_FA6] = "fa6",
77  [GDB_REGNO_FA7] = "fa7",
78  [GDB_REGNO_FS2] = "fs2",
79  [GDB_REGNO_FS3] = "fs3",
80  [GDB_REGNO_FS4] = "fs4",
81  [GDB_REGNO_FS5] = "fs5",
82  [GDB_REGNO_FS6] = "fs6",
83  [GDB_REGNO_FS7] = "fs7",
84  [GDB_REGNO_FS8] = "fs8",
85  [GDB_REGNO_FS9] = "fs9",
86  [GDB_REGNO_FS10] = "fs10",
87  [GDB_REGNO_FS11] = "fs11",
88  [GDB_REGNO_FT8] = "ft8",
89  [GDB_REGNO_FT9] = "ft9",
90  [GDB_REGNO_FT10] = "ft10",
91  [GDB_REGNO_FT11] = "ft11",
92 
93  #define DECLARE_CSR(csr_name, number)[(number) + GDB_REGNO_CSR0] = #csr_name,
94  #include "encoding.h"
95  #undef DECLARE_CSR
96 };
97 
99 {
100  RISCV_INFO(info);
101 
102  if (!info->custom_register_names.reg_names)
103  return;
104 
105  for (unsigned int i = 0; i < info->custom_register_names.num_entries; i++)
106  free(info->custom_register_names.reg_names[i]);
107  free(info->custom_register_names.reg_names);
108  info->custom_register_names.reg_names = NULL;
109 }
110 
111 static void free_reg_names(struct target *target)
112 {
113  RISCV_INFO(info);
114 
115  if (!info->reg_names)
116  return;
117 
118  for (unsigned int i = 0; i < GDB_REGNO_COUNT; ++i)
119  free(info->reg_names[i]);
120  free(info->reg_names);
121  info->reg_names = NULL;
122 
124 }
125 
126 static void init_custom_csr_names(const struct target *target)
127 {
128  RISCV_INFO(info);
129  range_list_t *entry;
130 
131  list_for_each_entry(entry, &info->expose_csr, list) {
132  if (!entry->name)
133  continue;
134  assert(entry->low == entry->high);
135  const unsigned int regno = entry->low + GDB_REGNO_CSR0;
136  assert(regno <= GDB_REGNO_CSR4095);
137  if (info->reg_names[regno])
138  return;
139  info->reg_names[regno] = strdup(entry->name);
140  if (!info->reg_names[regno])
141  LOG_ERROR("Failed to allocate memory for a register name");
142  }
143 }
144 
145 const char *riscv_reg_gdb_regno_name(const struct target *target, enum gdb_regno regno)
146 {
147  RISCV_INFO(info);
148 
149  if (regno >= GDB_REGNO_COUNT) {
150  assert(info->custom_register_names.reg_names);
151  assert(regno - GDB_REGNO_COUNT <= info->custom_register_names.num_entries);
152  return info->custom_register_names.reg_names[regno - GDB_REGNO_COUNT];
153  }
154 
155  if (!info->reg_names)
156  info->reg_names = calloc(GDB_REGNO_COUNT, sizeof(char *));
157 
158  if (info->reg_names[regno])
159  return info->reg_names[regno];
160  if (default_reg_names[regno])
161  return default_reg_names[regno];
162  if (regno <= GDB_REGNO_XPR31) {
163  info->reg_names[regno] = alloc_printf("x%d", regno - GDB_REGNO_ZERO);
164  if (!info->reg_names[regno])
165  LOG_ERROR("Failed to allocate memory for a register name");
166  return info->reg_names[regno];
167  }
168  if (regno <= GDB_REGNO_V31 && regno >= GDB_REGNO_V0) {
169  info->reg_names[regno] = alloc_printf("v%d", regno - GDB_REGNO_V0);
170  if (!info->reg_names[regno])
171  LOG_ERROR("Failed to allocate memory for a register name");
172  return info->reg_names[regno];
173  }
174  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095) {
176  if (!info->reg_names[regno])
177  info->reg_names[regno] = alloc_printf("csr%d", regno - GDB_REGNO_CSR0);
178  if (!info->reg_names[regno])
179  LOG_ERROR("Failed to allocate memory for a register name");
180  return info->reg_names[regno];
181  }
182  assert(!"Encountered uninitialized entry in reg_names table");
183 
184  return NULL;
185 }
186 
187 struct target *riscv_reg_impl_get_target(const struct reg *reg)
188 {
190  return ((const riscv_reg_info_t *)reg->arch_info)->target;
191 }
192 
193 static struct reg_feature *gdb_regno_feature(uint32_t regno)
194 {
195  if (regno <= GDB_REGNO_XPR31 || regno == GDB_REGNO_PC) {
196  static struct reg_feature feature_cpu = {
197  .name = "org.gnu.gdb.riscv.cpu"
198  };
199  return &feature_cpu;
200  }
201  if ((regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) ||
202  regno == GDB_REGNO_FFLAGS ||
203  regno == GDB_REGNO_FRM ||
204  regno == GDB_REGNO_FCSR) {
205  static struct reg_feature feature_fpu = {
206  .name = "org.gnu.gdb.riscv.fpu"
207  };
208  return &feature_fpu;
209  }
210  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31) {
211  static struct reg_feature feature_vector = {
212  .name = "org.gnu.gdb.riscv.vector"
213  };
214  return &feature_vector;
215  }
216  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095) {
217  static struct reg_feature feature_csr = {
218  .name = "org.gnu.gdb.riscv.csr"
219  };
220  return &feature_csr;
221  }
222  if (regno == GDB_REGNO_PRIV) {
223  static struct reg_feature feature_virtual = {
224  .name = "org.gnu.gdb.riscv.virtual"
225  };
226  return &feature_virtual;
227  }
228  assert(regno >= GDB_REGNO_COUNT);
229  static struct reg_feature feature_custom = {
230  .name = "org.gnu.gdb.riscv.custom"
231  };
232  return &feature_custom;
233 }
234 
235 static bool gdb_regno_caller_save(uint32_t regno)
236 {
237  return regno <= GDB_REGNO_XPR31 ||
238  regno == GDB_REGNO_PC ||
239  (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31);
240 }
241 
242 static struct reg_data_type *gdb_regno_reg_data_type(const struct target *target,
243  uint32_t regno)
244 {
245  if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) {
246  static struct reg_data_type type_ieee_single = {
248  .id = "ieee_single"
249  };
250  static struct reg_data_type type_ieee_double = {
252  .id = "ieee_double"
253  };
254  static struct reg_data_type_union_field single_double_fields[] = {
255  {"float", &type_ieee_single, single_double_fields + 1},
256  {"double", &type_ieee_double, NULL},
257  };
258  static struct reg_data_type_union single_double_union = {
259  .fields = single_double_fields
260  };
261  static struct reg_data_type type_ieee_single_double = {
263  .id = "FPU_FD",
264  .type_class = REG_TYPE_CLASS_UNION,
265  {.reg_type_union = &single_double_union}
266  };
267  return riscv_supports_extension(target, 'D') ?
268  &type_ieee_single_double :
269  &type_ieee_single;
270  }
271  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31) {
272  RISCV_INFO(info);
273  return &info->type_vector;
274  }
275  return NULL;
276 }
277 
278 static const char *gdb_regno_group(uint32_t regno)
279 {
280  if (regno <= GDB_REGNO_XPR31 ||
281  regno == GDB_REGNO_PC ||
282  regno == GDB_REGNO_PRIV)
283  return "general";
284  if ((regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) ||
285  regno == GDB_REGNO_FFLAGS ||
286  regno == GDB_REGNO_FRM ||
287  regno == GDB_REGNO_FCSR)
288  return "float";
289  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095)
290  return "csr";
291  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31)
292  return "vector";
293  assert(regno >= GDB_REGNO_COUNT);
294  return "custom";
295 }
296 
297 uint32_t gdb_regno_size(const struct target *target, uint32_t regno)
298 {
299  if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
300  return riscv_supports_extension(target, 'D') ? 64 : 32;
301  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31)
302  return riscv_vlenb(target) * 8;
303  if (regno == GDB_REGNO_PRIV)
304  return 8;
305  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095) {
306  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
307  switch (csr_number) {
308  case CSR_DCSR:
309  case CSR_MVENDORID:
310  case CSR_MCOUNTINHIBIT:
311 
312  case CSR_FFLAGS:
313  case CSR_FRM:
314  case CSR_FCSR:
315 
316  case CSR_SCOUNTEREN:
317  case CSR_MCOUNTEREN:
318  return 32;
319  }
320  }
321  return riscv_xlen(target);
322 }
323 
324 static bool vlenb_exists(const struct target *target)
325 {
326  return riscv_vlenb(target) != 0;
327 }
328 
329 static bool reg_exists(const struct target *target, uint32_t regno)
330 {
331  const struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
333  return reg->exist;
334 }
335 
336 static bool is_known_standard_csr(unsigned int csr_num)
337 {
338  static const bool is_csr_in_buf[GDB_REGNO_CSR4095 - GDB_REGNO_CSR0 + 1] = {
339  #define DECLARE_CSR(csr_name, number)[number] = true,
340  #include "encoding.h"
341  #undef DECLARE_CSR
342  };
343  assert(csr_num < ARRAY_SIZE(is_csr_in_buf));
344 
345  return is_csr_in_buf[csr_num];
346 }
347 
348 bool riscv_reg_impl_gdb_regno_exist(const struct target *target, uint32_t regno)
349 {
350  switch (regno) {
351  case GDB_REGNO_VLENB:
352  case GDB_REGNO_MTOPI:
353  case GDB_REGNO_MTOPEI:
354  assert(false
355  && "Existence of other registers is determined "
356  "depending on existence of these ones, so "
357  "whether these register exist or not should be "
358  "set explicitly.");
359  };
360 
361  if (regno <= GDB_REGNO_XPR15 ||
362  regno == GDB_REGNO_PC ||
363  regno == GDB_REGNO_PRIV)
364  return true;
365  if (regno > GDB_REGNO_XPR15 && regno <= GDB_REGNO_XPR31)
366  return !riscv_supports_extension(target, 'E');
367  if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
368  return riscv_supports_extension(target, 'F');
369  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31)
370  return vlenb_exists(target);
371  if (regno >= GDB_REGNO_COUNT)
372  return true;
373  assert(regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095);
374  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
375  switch (csr_number) {
376  case CSR_FFLAGS:
377  case CSR_FRM:
378  case CSR_FCSR:
379  return riscv_supports_extension(target, 'F');
380  case CSR_VSTART:
381  case CSR_VXSAT:
382  case CSR_VXRM:
383  case CSR_VL:
384  case CSR_VCSR:
385  case CSR_VTYPE:
386  return vlenb_exists(target);
387  case CSR_SCOUNTEREN:
388  case CSR_SSTATUS:
389  case CSR_STVEC:
390  case CSR_SIP:
391  case CSR_SIE:
392  case CSR_SSCRATCH:
393  case CSR_SEPC:
394  case CSR_SCAUSE:
395  case CSR_STVAL:
396  case CSR_SATP:
397  return riscv_supports_extension(target, 'S');
398  case CSR_MEDELEG:
399  case CSR_MIDELEG:
400  /* "In systems with only M-mode, or with both M-mode and
401  * U-mode but without U-mode trap support, the medeleg and
402  * mideleg registers should not exist." */
403  return riscv_supports_extension(target, 'S') ||
405 
406  case CSR_PMPCFG1:
407  case CSR_PMPCFG3:
408  case CSR_CYCLEH:
409  case CSR_TIMEH:
410  case CSR_INSTRETH:
411  case CSR_HPMCOUNTER3H:
412  case CSR_HPMCOUNTER4H:
413  case CSR_HPMCOUNTER5H:
414  case CSR_HPMCOUNTER6H:
415  case CSR_HPMCOUNTER7H:
416  case CSR_HPMCOUNTER8H:
417  case CSR_HPMCOUNTER9H:
418  case CSR_HPMCOUNTER10H:
419  case CSR_HPMCOUNTER11H:
420  case CSR_HPMCOUNTER12H:
421  case CSR_HPMCOUNTER13H:
422  case CSR_HPMCOUNTER14H:
423  case CSR_HPMCOUNTER15H:
424  case CSR_HPMCOUNTER16H:
425  case CSR_HPMCOUNTER17H:
426  case CSR_HPMCOUNTER18H:
427  case CSR_HPMCOUNTER19H:
428  case CSR_HPMCOUNTER20H:
429  case CSR_HPMCOUNTER21H:
430  case CSR_HPMCOUNTER22H:
431  case CSR_HPMCOUNTER23H:
432  case CSR_HPMCOUNTER24H:
433  case CSR_HPMCOUNTER25H:
434  case CSR_HPMCOUNTER26H:
435  case CSR_HPMCOUNTER27H:
436  case CSR_HPMCOUNTER28H:
437  case CSR_HPMCOUNTER29H:
438  case CSR_HPMCOUNTER30H:
439  case CSR_HPMCOUNTER31H:
440  case CSR_MCYCLEH:
441  case CSR_MINSTRETH:
442  case CSR_MHPMCOUNTER4H:
443  case CSR_MHPMCOUNTER5H:
444  case CSR_MHPMCOUNTER6H:
445  case CSR_MHPMCOUNTER7H:
446  case CSR_MHPMCOUNTER8H:
447  case CSR_MHPMCOUNTER9H:
448  case CSR_MHPMCOUNTER10H:
449  case CSR_MHPMCOUNTER11H:
450  case CSR_MHPMCOUNTER12H:
451  case CSR_MHPMCOUNTER13H:
452  case CSR_MHPMCOUNTER14H:
453  case CSR_MHPMCOUNTER15H:
454  case CSR_MHPMCOUNTER16H:
455  case CSR_MHPMCOUNTER17H:
456  case CSR_MHPMCOUNTER18H:
457  case CSR_MHPMCOUNTER19H:
458  case CSR_MHPMCOUNTER20H:
459  case CSR_MHPMCOUNTER21H:
460  case CSR_MHPMCOUNTER22H:
461  case CSR_MHPMCOUNTER23H:
462  case CSR_MHPMCOUNTER24H:
463  case CSR_MHPMCOUNTER25H:
464  case CSR_MHPMCOUNTER26H:
465  case CSR_MHPMCOUNTER27H:
466  case CSR_MHPMCOUNTER28H:
467  case CSR_MHPMCOUNTER29H:
468  case CSR_MHPMCOUNTER30H:
469  case CSR_MHPMCOUNTER31H:
470  return riscv_xlen(target) == 32;
471  case CSR_MCOUNTEREN:
472  return riscv_supports_extension(target, 'U');
473  /* Interrupts M-Mode CSRs. */
474  case CSR_MISELECT:
475  case CSR_MIREG:
476  case CSR_MVIEN:
477  case CSR_MVIP:
478  case CSR_MIEH:
479  case CSR_MIPH:
481  case CSR_MIDELEGH:
482  case CSR_MVIENH:
483  case CSR_MVIPH:
484  return reg_exists(target, GDB_REGNO_MTOPI) &&
485  riscv_xlen(target) == 32 &&
487  /* Interrupts S-Mode CSRs. */
488  case CSR_SISELECT:
489  case CSR_SIREG:
490  case CSR_STOPI:
491  return reg_exists(target, GDB_REGNO_MTOPI) &&
493  case CSR_STOPEI:
496  case CSR_SIEH:
497  case CSR_SIPH:
498  return reg_exists(target, GDB_REGNO_MTOPI) &&
499  riscv_xlen(target) == 32 &&
501  /* Interrupts Hypervisor and VS CSRs. */
502  case CSR_HVIEN:
503  case CSR_HVICTL:
504  case CSR_HVIPRIO1:
505  case CSR_HVIPRIO2:
506  case CSR_VSISELECT:
507  case CSR_VSIREG:
508  case CSR_VSTOPI:
509  return reg_exists(target, GDB_REGNO_MTOPI) &&
511  case CSR_VSTOPEI:
514  case CSR_HIDELEGH:
515  case CSR_HVIENH:
516  case CSR_HVIPH:
517  case CSR_HVIPRIO1H:
518  case CSR_HVIPRIO2H:
519  case CSR_VSIEH:
520  case CSR_VSIPH:
521  return reg_exists(target, GDB_REGNO_MTOPI) &&
522  riscv_xlen(target) == 32 &&
524  }
525  return is_known_standard_csr(csr_number);
526 }
527 
528 static unsigned int gdb_regno_custom_number(const struct target *target, uint32_t regno)
529 {
530  if (regno < GDB_REGNO_COUNT)
531  return 0;
532 
533  RISCV_INFO(info);
534  assert(!list_empty(&info->expose_custom));
536  unsigned int regno_start = GDB_REGNO_COUNT;
537  unsigned int start = 0;
538  unsigned int offset = 0;
539  list_for_each_entry(range, &info->expose_custom, list) {
540  start = range->low;
541  assert(regno >= regno_start);
542  offset = regno - regno_start;
543  const unsigned int regs_in_range = range->high - range->low + 1;
544  if (offset < regs_in_range)
545  break;
546  regno_start += regs_in_range;
547  }
548  return start + offset;
549 }
550 
552  uint32_t number)
553 {
554  assert(target->reg_cache);
555  assert(target->reg_cache->reg_list);
556  assert(number < target->reg_cache->num_regs);
557  return &target->reg_cache->reg_list[number];
558 }
559 
560 static int resize_reg(const struct target *target, uint32_t regno, bool exist,
561  uint32_t size)
562 {
563  struct reg *reg = riscv_reg_impl_cache_entry(target, regno);
565  free(reg->value);
566  reg->size = size;
567  reg->exist = exist;
568  if (reg->exist) {
569  reg->value = malloc(DIV_ROUND_UP(reg->size, 8));
570  if (!reg->value) {
571  LOG_ERROR("Failed to allocate memory.");
572  return ERROR_FAIL;
573  }
574  } else {
575  reg->value = NULL;
576  }
578  return ERROR_OK;
579 }
580 
581 int riscv_reg_impl_set_exist(const struct target *target, uint32_t regno, bool exist)
582 {
583  const struct reg *reg = riscv_reg_impl_cache_entry(target, regno);
585  return resize_reg(target, regno, exist, reg->size);
586 }
587 
588 int riscv_reg_impl_init_cache_entry(struct target *target, uint32_t regno,
589  bool exist, const struct reg_arch_type *reg_type)
590 {
591  struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
593  return ERROR_OK;
594  reg->number = regno;
595  reg->type = reg_type;
596  reg->dirty = false;
597  reg->valid = false;
598  reg->hidden = false;
600  reg->feature = gdb_regno_feature(regno);
603  reg->group = gdb_regno_group(regno);
604  if (regno < GDB_REGNO_COUNT) {
605  RISCV_INFO(info);
606  reg->arch_info = &info->shared_reg_info;
607  } else {
608  reg->arch_info = calloc(1, sizeof(riscv_reg_info_t));
609  if (!reg->arch_info) {
610  LOG_ERROR("Out of memory.");
611  return ERROR_FAIL;
612  }
613  riscv_reg_info_t * const reg_arch_info = reg->arch_info;
614  reg_arch_info->target = target;
615  reg_arch_info->custom_number = gdb_regno_custom_number(target, regno);
616  }
617  return resize_reg(target, regno, exist, gdb_regno_size(target, regno));
618 }
619 
620 static int init_custom_register_names(struct list_head *expose_custom,
621  struct reg_name_table *custom_register_names)
622 {
623  unsigned int custom_regs_num = 0;
624  if (!list_empty(expose_custom)) {
625  range_list_t *entry;
626  list_for_each_entry(entry, expose_custom, list)
627  custom_regs_num += entry->high - entry->low + 1;
628  }
629 
630  if (!custom_regs_num)
631  return ERROR_OK;
632 
633  custom_register_names->reg_names = calloc(custom_regs_num, sizeof(char *));
634  if (!custom_register_names->reg_names) {
635  LOG_ERROR("Failed to allocate memory for custom_register_names->reg_names");
636  return ERROR_FAIL;
637  }
638  custom_register_names->num_entries = custom_regs_num;
639  char **reg_names = custom_register_names->reg_names;
641  unsigned int next_custom_reg_index = 0;
642  list_for_each_entry(range, expose_custom, list) {
643  for (unsigned int custom_number = range->low; custom_number <= range->high; ++custom_number) {
644  if (range->name)
645  reg_names[next_custom_reg_index] = strdup(range->name);
646  else
647  reg_names[next_custom_reg_index] =
648  alloc_printf("custom%d", custom_number);
649 
650  if (!reg_names[next_custom_reg_index]) {
651  LOG_ERROR("Failed to allocate memory for a register name");
652  return ERROR_FAIL;
653  }
654  ++next_custom_reg_index;
655  }
656  }
657  return ERROR_OK;
658 }
659 
661 {
662  RISCV_INFO(info);
663 
665 
666  target->reg_cache = calloc(1, sizeof(*target->reg_cache));
667  if (!target->reg_cache) {
668  LOG_TARGET_ERROR(target, "Failed to allocate memory for target->reg_cache");
669  return ERROR_FAIL;
670  }
671  target->reg_cache->name = "RISC-V Registers";
672 
673  if (init_custom_register_names(&info->expose_custom, &info->custom_register_names) != ERROR_OK) {
674  LOG_TARGET_ERROR(target, "init_custom_register_names failed");
675  return ERROR_FAIL;
676  }
677 
678  target->reg_cache->num_regs = GDB_REGNO_COUNT + info->custom_register_names.num_entries;
679  LOG_TARGET_DEBUG(target, "create register cache for %d registers",
681 
683  calloc(target->reg_cache->num_regs, sizeof(struct reg));
684  if (!target->reg_cache->reg_list) {
685  LOG_TARGET_ERROR(target, "Failed to allocate memory for target->reg_cache->reg_list");
686  return ERROR_FAIL;
687  }
688  return ERROR_OK;
689 }
690 
692 {
693  RISCV_INFO(info);
694  range_list_t *entry;
695  list_for_each_entry(entry, &info->expose_csr, list) {
696  assert(entry->low <= entry->high);
697  assert(entry->high <= GDB_REGNO_CSR4095 - GDB_REGNO_CSR0);
698  const enum gdb_regno last_regno = GDB_REGNO_CSR0 + entry->high;
699  for (enum gdb_regno regno = GDB_REGNO_CSR0 + entry->low;
700  regno <= last_regno; ++regno) {
701  struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
702  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
703  if (reg->exist) {
705  "Not exposing CSR %d: register already exists.",
706  csr_number);
707  continue;
708  }
709  if (riscv_reg_impl_set_exist(target, regno, /*exist*/ true) != ERROR_OK)
710  return ERROR_FAIL;
711  LOG_TARGET_DEBUG(target, "Exposing additional CSR %d (name=%s)",
712  csr_number, reg->name);
713  }
714  }
715  return ERROR_OK;
716 }
717 
719 {
720  RISCV_INFO(info);
721  range_list_t *entry;
722  list_for_each_entry(entry, &info->hide_csr, list) {
723  assert(entry->high <= GDB_REGNO_CSR4095 - GDB_REGNO_CSR0);
724  const enum gdb_regno last_regno = GDB_REGNO_CSR0 + entry->high;
725  for (enum gdb_regno regno = GDB_REGNO_CSR0 + entry->low;
726  regno <= last_regno; ++regno) {
727  struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
728  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
729  if (!reg->exist) {
731  "Not hiding CSR %d: register does not exist.",
732  csr_number);
733  continue;
734  }
735  LOG_TARGET_DEBUG(target, "Hiding CSR %d (name=%s).", csr_number, reg->name);
736  reg->hidden = true;
737  }
738  }
739 }
740 
742 {
744  /* Free the shared structure use for most registers. */
745  if (!target->reg_cache)
746  return;
747  if (target->reg_cache->reg_list) {
748  for (unsigned int i = GDB_REGNO_COUNT; i < target->reg_cache->num_regs; i++)
749  free(target->reg_cache->reg_list[i].arch_info);
750  for (unsigned int i = 0; i < target->reg_cache->num_regs; i++)
751  free(target->reg_cache->reg_list[i].value);
752  free(target->reg_cache->reg_list);
753  }
754  free(target->reg_cache);
755  target->reg_cache = NULL;
756 }
757 
759 {
760  if (!target->reg_cache)
761  return ERROR_OK;
762 
763  LOG_TARGET_DEBUG_IO(target, "Flushing register cache");
764 
765  /* Writing non-GPR registers may require progbuf execution, and some GPRs
766  * may become dirty in the process (e.g. S0, S1). For that reason, flush
767  * registers in reverse order, so that GPRs are flushed last.
768  */
769  for (unsigned int number = target->reg_cache->num_regs; number-- > 0; ) {
771  if (reg->valid && reg->dirty) {
773 
774  LOG_TARGET_DEBUG(target, "%s is dirty; write back 0x%" PRIx64,
775  reg->name, value);
777  return ERROR_FAIL;
778  }
779  }
780  LOG_TARGET_DEBUG_IO(target, "Flush of register cache completed");
781  return ERROR_OK;
782 }
783 
797  enum gdb_regno regid, riscv_reg_t value, bool write_through)
798 {
799  RISCV_INFO(r);
800  assert(r);
801  if (r->dtm_version == DTM_DTMCS_VERSION_0_11)
802  return riscv011_set_register(target, regid, value);
803 
804  keep_alive();
805 
806  if (regid == GDB_REGNO_PC) {
807  return riscv_set_or_write_register(target, GDB_REGNO_DPC, value, write_through);
808  } else if (regid == GDB_REGNO_PRIV) {
809  riscv_reg_t dcsr;
810 
812  return ERROR_FAIL;
815  return riscv_set_or_write_register(target, GDB_REGNO_DCSR, dcsr, write_through);
816  }
817 
818  struct reg *reg = riscv_reg_impl_cache_entry(target, regid);
820 
821  if (!reg->exist) {
822  LOG_TARGET_DEBUG(target, "Register %s does not exist.", reg->name);
823  return ERROR_FAIL;
824  }
825 
826  if (target->state != TARGET_HALTED) {
828  "Target not halted, writing to target: %s <- 0x%" PRIx64,
829  reg->name, value);
830  return riscv013_set_register(target, regid, value);
831  }
832 
833  const bool need_to_write = !reg->valid || reg->dirty ||
834  value != buf_get_u64(reg->value, 0, reg->size);
835  const bool cacheable = riscv_reg_impl_gdb_regno_cacheable(regid, need_to_write);
836 
837  if (!cacheable || (write_through && need_to_write)) {
839  "Writing to target: %s <- 0x%" PRIx64 " (cacheable=%s, valid=%s, dirty=%s)",
840  reg->name, value, cacheable ? "true" : "false",
841  reg->valid ? "true" : "false",
842  reg->dirty ? "true" : "false");
843  if (riscv013_set_register(target, regid, value) != ERROR_OK)
844  return ERROR_FAIL;
845 
846  reg->dirty = false;
847  } else {
848  reg->dirty = need_to_write;
849  }
850 
851  buf_set_u64(reg->value, 0, reg->size, value);
852  reg->valid = cacheable;
853 
855  "Wrote 0x%" PRIx64 " to %s (cacheable=%s, valid=%s, dirty=%s)",
856  value, reg->name, cacheable ? "true" : "false",
857  reg->valid ? "true" : "false",
858  reg->dirty ? "true" : "false");
859  return ERROR_OK;
860 }
861 
862 bool riscv_reg_cache_any_dirty(const struct target *target, int log_level)
863 {
864  bool any_dirty = false;
865 
866  if (!target->reg_cache)
867  return any_dirty;
868 
869  for (unsigned int number = 0; number < target->reg_cache->num_regs; ++number) {
870  const struct reg * const reg = riscv_reg_impl_cache_entry(target, number);
872  if (reg->dirty) {
873  log_printf_lf(log_level, __FILE__, __LINE__, __func__,
874  "[%s] Register %s is dirty!", target_name(target), reg->name);
875  any_dirty = true;
876  }
877  }
878  return any_dirty;
879 }
880 
882 {
883  if (!target->reg_cache)
884  return;
885 
886  LOG_TARGET_DEBUG(target, "Invalidating register cache.");
888 }
889 
900 int riscv_reg_set(struct target *target, enum gdb_regno regid,
902 {
904  /* write_through */ false);
905 }
906 
917 int riscv_reg_write(struct target *target, enum gdb_regno regid,
919 {
921  /* write_through */ true);
922 }
923 
935  enum gdb_regno regid)
936 {
937  RISCV_INFO(r);
938  assert(r);
939  if (r->dtm_version == DTM_DTMCS_VERSION_0_11)
940  return riscv011_get_register(target, value, regid);
941 
942  keep_alive();
943 
944  if (regid == GDB_REGNO_PC)
946 
947  struct reg *reg = riscv_reg_impl_cache_entry(target, regid);
949  if (!reg->exist) {
950  LOG_TARGET_DEBUG(target, "Register %s does not exist.", reg->name);
951  return ERROR_FAIL;
952  }
953 
954  if (reg->valid) {
955  *value = buf_get_u64(reg->value, 0, reg->size);
956  LOG_TARGET_DEBUG(target, "Read %s: 0x%" PRIx64 " (cached)", reg->name,
957  *value);
958  return ERROR_OK;
959  }
960 
961  LOG_TARGET_DEBUG(target, "Reading %s from target", reg->name);
962  if (riscv013_get_register(target, value, regid) != ERROR_OK)
963  return ERROR_FAIL;
964 
965  buf_set_u64(reg->value, 0, reg->size, *value);
966  reg->valid = riscv_reg_impl_gdb_regno_cacheable(regid, /* is write? */ false) &&
968  reg->dirty = false;
969 
970  LOG_TARGET_DEBUG(target, "Read %s: 0x%" PRIx64, reg->name, *value);
971  return ERROR_OK;
972 }
static uint64_t buf_get_u64(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 64-bit word.
Definition: binarybuffer.h:134
static void buf_set_u64(uint8_t *_buffer, unsigned int first, unsigned int num, uint64_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:65
#define VIRT_PRIV_PRV
#define CSR_DCSR_V
#define DTM_DTMCS_VERSION_0_11
#define CSR_DCSR
#define VIRT_PRIV_V
#define CSR_DCSR_PRV
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
#define CSR_MHPMCOUNTER17H
Definition: encoding.h:3199
#define CSR_HVIEN
Definition: encoding.h:2875
#define CSR_MCOUNTEREN
Definition: encoding.h:2915
#define CSR_HPMCOUNTER25H
Definition: encoding.h:3135
#define CSR_SIPH
Definition: encoding.h:3094
#define CSR_MHPMCOUNTER7H
Definition: encoding.h:3189
#define CSR_MHPMCOUNTER27H
Definition: encoding.h:3209
#define CSR_PMPCFG1
Definition: encoding.h:2935
#define CSR_HPMCOUNTER22H
Definition: encoding.h:3132
#define CSR_MHPMCOUNTER18H
Definition: encoding.h:3200
#define CSR_VCSR
Definition: encoding.h:2795
#define CSR_MHPMCOUNTER21H
Definition: encoding.h:3203
#define CSR_HVIPRIO2
Definition: encoding.h:2886
#define CSR_HPMCOUNTER15H
Definition: encoding.h:3125
#define CSR_MIEH
Definition: encoding.h:3144
#define CSR_STOPI
Definition: encoding.h:2893
#define CSR_HPMCOUNTER12H
Definition: encoding.h:3122
#define CSR_HPMCOUNTER19H
Definition: encoding.h:3129
#define CSR_MIREG
Definition: encoding.h:2932
#define CSR_TIMEH
Definition: encoding.h:3111
#define CSR_MHPMCOUNTER28H
Definition: encoding.h:3210
#define CSR_HPMCOUNTER3H
Definition: encoding.h:3113
#define CSR_MHPMCOUNTER9H
Definition: encoding.h:3191
#define CSR_MVIEN
Definition: encoding.h:2916
#define CSR_SEPC
Definition: encoding.h:2845
#define CSR_MHPMCOUNTER29H
Definition: encoding.h:3211
#define CSR_MHPMCOUNTER10H
Definition: encoding.h:3192
#define CSR_HPMCOUNTER4H
Definition: encoding.h:3114
#define CSR_PMPCFG3
Definition: encoding.h:2937
#define CSR_VSTOPEI
Definition: encoding.h:2866
#define CSR_MHPMCOUNTER11H
Definition: encoding.h:3193
#define CSR_HVIPH
Definition: encoding.h:3103
#define CSR_HVIENH
Definition: encoding.h:3101
#define CSR_SCAUSE
Definition: encoding.h:2846
#define CSR_HPMCOUNTER9H
Definition: encoding.h:3119
#define CSR_SIEH
Definition: encoding.h:3093
#define CSR_MHPMCOUNTER12H
Definition: encoding.h:3194
#define CSR_HPMCOUNTER31H
Definition: encoding.h:3141
#define CSR_SSCRATCH
Definition: encoding.h:2844
#define CSR_MHPMCOUNTER25H
Definition: encoding.h:3207
#define CSR_HPMCOUNTER6H
Definition: encoding.h:3116
#define CSR_MHPMCOUNTER26H
Definition: encoding.h:3208
#define CSR_HPMCOUNTER27H
Definition: encoding.h:3137
#define CSR_MVIP
Definition: encoding.h:2917
#define CSR_HPMCOUNTER17H
Definition: encoding.h:3127
#define CSR_MHPMCOUNTER5H
Definition: encoding.h:3187
#define CSR_HPMCOUNTER21H
Definition: encoding.h:3131
#define CSR_SSTATUS
Definition: encoding.h:2833
#define CSR_MHPMCOUNTER24H
Definition: encoding.h:3206
#define CSR_HPMCOUNTER18H
Definition: encoding.h:3128
#define CSR_STVAL
Definition: encoding.h:2847
#define CSR_MHPMCOUNTER8H
Definition: encoding.h:3190
#define CSR_SISELECT
Definition: encoding.h:2850
#define CSR_HPMCOUNTER14H
Definition: encoding.h:3124
#define CSR_MHPMCOUNTER20H
Definition: encoding.h:3202
#define CSR_VSIREG
Definition: encoding.h:2865
#define CSR_MVIPH
Definition: encoding.h:3146
#define CSR_VSTOPI
Definition: encoding.h:2891
#define CSR_HPMCOUNTER28H
Definition: encoding.h:3138
#define CSR_SATP
Definition: encoding.h:2853
#define CSR_MHPMCOUNTER31H
Definition: encoding.h:3213
#define CSR_STOPEI
Definition: encoding.h:2852
#define CSR_MCOUNTINHIBIT
Definition: encoding.h:2923
#define CSR_VTYPE
Definition: encoding.h:2831
#define CSR_VSIEH
Definition: encoding.h:3096
#define CSR_MHPMCOUNTER6H
Definition: encoding.h:3188
#define CSR_HPMCOUNTER16H
Definition: encoding.h:3126
#define CSR_MHPMCOUNTER23H
Definition: encoding.h:3205
#define CSR_MINSTRETH
Definition: encoding.h:3184
#define CSR_HPMCOUNTER20H
Definition: encoding.h:3130
#define CSR_FRM
Definition: encoding.h:2790
#define CSR_MHPMCOUNTER15H
Definition: encoding.h:3197
#define CSR_STVEC
Definition: encoding.h:2837
#define CSR_MEDELEG
Definition: encoding.h:2911
#define CSR_HPMCOUNTER23H
Definition: encoding.h:3133
#define CSR_VL
Definition: encoding.h:2830
#define CSR_HPMCOUNTER30H
Definition: encoding.h:3140
#define CSR_MHPMCOUNTER14H
Definition: encoding.h:3196
#define CSR_MIPH
Definition: encoding.h:3152
#define CSR_FCSR
Definition: encoding.h:2791
#define CSR_HPMCOUNTER5H
Definition: encoding.h:3115
#define CSR_HPMCOUNTER13H
Definition: encoding.h:3123
#define CSR_MVENDORID
Definition: encoding.h:3087
#define CSR_SIP
Definition: encoding.h:2848
#define CSR_SIREG
Definition: encoding.h:2851
#define CSR_HPMCOUNTER7H
Definition: encoding.h:3117
#define CSR_MHPMCOUNTER13H
Definition: encoding.h:3195
#define CSR_MIDELEGH
Definition: encoding.h:3143
#define CSR_HPMCOUNTER26H
Definition: encoding.h:3136
#define CSR_SCOUNTEREN
Definition: encoding.h:2838
#define CSR_VXRM
Definition: encoding.h:2794
#define CSR_MHPMCOUNTER4H
Definition: encoding.h:3186
#define CSR_HVIPRIO1H
Definition: encoding.h:3104
#define CSR_HPMCOUNTER8H
Definition: encoding.h:3118
#define CSR_MCYCLEH
Definition: encoding.h:3183
#define CSR_MIDELEG
Definition: encoding.h:2912
#define CSR_VXSAT
Definition: encoding.h:2793
#define CSR_HVIPRIO2H
Definition: encoding.h:3105
#define CSR_VSIPH
Definition: encoding.h:3097
#define CSR_VSISELECT
Definition: encoding.h:2864
#define CSR_HVIPRIO1
Definition: encoding.h:2885
#define CSR_MISELECT
Definition: encoding.h:2931
#define CSR_HPMCOUNTER11H
Definition: encoding.h:3121
#define CSR_HPMCOUNTER24H
Definition: encoding.h:3134
#define CSR_INSTRETH
Definition: encoding.h:3112
#define CSR_FFLAGS
Definition: encoding.h:2789
#define CSR_HVICTL
Definition: encoding.h:2876
#define CSR_MVIENH
Definition: encoding.h:3145
#define CSR_MHPMCOUNTER22H
Definition: encoding.h:3204
#define CSR_HPMCOUNTER29H
Definition: encoding.h:3139
#define CSR_HPMCOUNTER10H
Definition: encoding.h:3120
#define CSR_HIDELEGH
Definition: encoding.h:3100
#define CSR_MHPMCOUNTER19H
Definition: encoding.h:3201
#define CSR_CYCLEH
Definition: encoding.h:3110
#define CSR_MHPMCOUNTER16H
Definition: encoding.h:3198
#define CSR_VSTART
Definition: encoding.h:2792
#define CSR_MHPMCOUNTER30H
Definition: encoding.h:3212
#define CSR_SIE
Definition: encoding.h:2836
enum esirisc_reg_num number
Definition: esirisc.c:87
static uint64_t set_field(uint64_t reg, uint64_t mask, uint64_t val)
Definition: field_helpers.h:21
static uint64_t get_field(uint64_t reg, uint64_t mask)
Definition: field_helpers.h:9
gdb_regno
Definition: gdb_regs.h:10
@ GDB_REGNO_DPC
Definition: gdb_regs.h:99
@ GDB_REGNO_S8
Definition: gdb_regs.h:37
@ GDB_REGNO_FS7
Definition: gdb_regs.h:72
@ GDB_REGNO_FS10
Definition: gdb_regs.h:75
@ GDB_REGNO_MTOPI
Definition: gdb_regs.h:110
@ GDB_REGNO_CSR0
Definition: gdb_regs.h:82
@ GDB_REGNO_FS8
Definition: gdb_regs.h:73
@ GDB_REGNO_FS6
Definition: gdb_regs.h:71
@ GDB_REGNO_S4
Definition: gdb_regs.h:33
@ GDB_REGNO_S11
Definition: gdb_regs.h:40
@ GDB_REGNO_MTOPEI
Definition: gdb_regs.h:111
@ GDB_REGNO_ZERO
Definition: gdb_regs.h:11
@ GDB_REGNO_FRM
Definition: gdb_regs.h:85
@ GDB_REGNO_S5
Definition: gdb_regs.h:34
@ GDB_REGNO_T5
Definition: gdb_regs.h:43
@ GDB_REGNO_T6
Definition: gdb_regs.h:44
@ GDB_REGNO_A4
Definition: gdb_regs.h:26
@ GDB_REGNO_GP
Definition: gdb_regs.h:14
@ GDB_REGNO_FA3
Definition: gdb_regs.h:62
@ GDB_REGNO_S7
Definition: gdb_regs.h:36
@ GDB_REGNO_A1
Definition: gdb_regs.h:23
@ GDB_REGNO_FT3
Definition: gdb_regs.h:52
@ GDB_REGNO_A5
Definition: gdb_regs.h:27
@ GDB_REGNO_T2
Definition: gdb_regs.h:18
@ GDB_REGNO_S1
Definition: gdb_regs.h:21
@ GDB_REGNO_XPR15
Definition: gdb_regs.h:28
@ GDB_REGNO_T3
Definition: gdb_regs.h:41
@ GDB_REGNO_SP
Definition: gdb_regs.h:13
@ GDB_REGNO_FA2
Definition: gdb_regs.h:61
@ GDB_REGNO_FA0
Definition: gdb_regs.h:59
@ GDB_REGNO_FPR31
Definition: gdb_regs.h:81
@ GDB_REGNO_FPR0
Definition: gdb_regs.h:48
@ GDB_REGNO_FT5
Definition: gdb_regs.h:54
@ GDB_REGNO_V0
Definition: gdb_regs.h:118
@ GDB_REGNO_FCSR
Definition: gdb_regs.h:83
@ GDB_REGNO_A6
Definition: gdb_regs.h:29
@ GDB_REGNO_FP
Definition: gdb_regs.h:20
@ GDB_REGNO_FA1
Definition: gdb_regs.h:60
@ GDB_REGNO_XPR31
Definition: gdb_regs.h:45
@ GDB_REGNO_A0
Definition: gdb_regs.h:22
@ GDB_REGNO_FS4
Definition: gdb_regs.h:69
@ GDB_REGNO_FT9
Definition: gdb_regs.h:78
@ GDB_REGNO_FT2
Definition: gdb_regs.h:51
@ GDB_REGNO_FA4
Definition: gdb_regs.h:63
@ GDB_REGNO_FT7
Definition: gdb_regs.h:56
@ GDB_REGNO_FS0
Definition: gdb_regs.h:57
@ GDB_REGNO_FT6
Definition: gdb_regs.h:55
@ GDB_REGNO_FS9
Definition: gdb_regs.h:74
@ GDB_REGNO_FT1
Definition: gdb_regs.h:50
@ GDB_REGNO_FT8
Definition: gdb_regs.h:77
@ GDB_REGNO_FT11
Definition: gdb_regs.h:80
@ GDB_REGNO_A2
Definition: gdb_regs.h:24
@ GDB_REGNO_FS3
Definition: gdb_regs.h:68
@ GDB_REGNO_A7
Definition: gdb_regs.h:30
@ GDB_REGNO_RA
Definition: gdb_regs.h:12
@ GDB_REGNO_S9
Definition: gdb_regs.h:38
@ GDB_REGNO_PC
Definition: gdb_regs.h:47
@ GDB_REGNO_T4
Definition: gdb_regs.h:42
@ GDB_REGNO_VLENB
Definition: gdb_regs.h:90
@ GDB_REGNO_FA5
Definition: gdb_regs.h:64
@ GDB_REGNO_V31
Definition: gdb_regs.h:125
@ GDB_REGNO_FA6
Definition: gdb_regs.h:65
@ GDB_REGNO_FFLAGS
Definition: gdb_regs.h:84
@ GDB_REGNO_S10
Definition: gdb_regs.h:39
@ GDB_REGNO_FT10
Definition: gdb_regs.h:79
@ GDB_REGNO_PRIV
Definition: gdb_regs.h:113
@ GDB_REGNO_S2
Definition: gdb_regs.h:31
@ GDB_REGNO_CSR4095
Definition: gdb_regs.h:112
@ GDB_REGNO_FS1
Definition: gdb_regs.h:58
@ GDB_REGNO_FA7
Definition: gdb_regs.h:66
@ GDB_REGNO_TP
Definition: gdb_regs.h:15
@ GDB_REGNO_FS2
Definition: gdb_regs.h:67
@ GDB_REGNO_FT0
Definition: gdb_regs.h:49
@ GDB_REGNO_COUNT
Definition: gdb_regs.h:126
@ GDB_REGNO_T1
Definition: gdb_regs.h:17
@ GDB_REGNO_S6
Definition: gdb_regs.h:35
@ GDB_REGNO_FT4
Definition: gdb_regs.h:53
@ GDB_REGNO_S3
Definition: gdb_regs.h:32
@ GDB_REGNO_T0
Definition: gdb_regs.h:16
@ GDB_REGNO_FS11
Definition: gdb_regs.h:76
@ GDB_REGNO_DCSR
Definition: gdb_regs.h:100
@ GDB_REGNO_A3
Definition: gdb_regs.h:25
@ GDB_REGNO_FS5
Definition: gdb_regs.h:70
static int list_empty(const struct list_head *head)
Definition: list.h:61
#define list_for_each_entry(p, h, field)
Definition: list.h:155
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:201
void keep_alive(void)
Definition: log.c:437
static int64_t start
Definition: log.c:38
char * alloc_printf(const char *format,...)
Definition: log.c:386
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:173
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:164
#define LOG_TARGET_DEBUG_IO(target, fmt_str,...)
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define ERROR_OK
Definition: log.h:182
void register_cache_invalidate(struct reg_cache *cache)
Marks the contents of the register cache as invalid (and clean).
Definition: register.c:94
reg_type
Definition: register.h:19
@ REG_TYPE_IEEE_DOUBLE
Definition: register.h:37
@ REG_TYPE_ARCH_DEFINED
Definition: register.h:38
@ REG_TYPE_IEEE_SINGLE
Definition: register.h:36
@ REG_TYPE_CLASS_UNION
Definition: register.h:94
int riscv011_get_register(struct target *target, riscv_reg_t *value, enum gdb_regno regid)
Definition: riscv-011.c:1343
int riscv011_set_register(struct target *target, enum gdb_regno regid, riscv_reg_t value)
Definition: riscv-011.c:1391
int riscv013_set_register(struct target *target, enum gdb_regno rid, riscv_reg_t value)
Definition: riscv-013.c:5185
int riscv013_get_register(struct target *target, riscv_reg_t *value, enum gdb_regno rid)
Definition: riscv-013.c:5156
unsigned int riscv_xlen(const struct target *target)
Definition: riscv.c:6147
bool riscv_supports_extension(const struct target *target, char letter)
Definition: riscv.c:6134
unsigned int riscv_vlenb(const struct target *target)
Definition: riscv.c:6153
#define RISCV_INFO(R)
Definition: riscv.h:427
uint64_t riscv_reg_t
Definition: riscv.h:45
int riscv_reg_impl_set_exist(const struct target *target, uint32_t regno, bool exist)
Mark register as existing or not.
Definition: riscv_reg.c:581
static struct reg_feature * gdb_regno_feature(uint32_t regno)
Definition: riscv_reg.c:193
static int riscv_set_or_write_register(struct target *target, enum gdb_regno regid, riscv_reg_t value, bool write_through)
This function is used internally by functions that change register values.
Definition: riscv_reg.c:796
void riscv_reg_impl_hide_csrs(const struct target *target)
Hide additional CSRs, as specified by riscv_info_t::hide_csr list.
Definition: riscv_reg.c:718
static int resize_reg(const struct target *target, uint32_t regno, bool exist, uint32_t size)
Definition: riscv_reg.c:560
bool riscv_reg_impl_gdb_regno_exist(const struct target *target, uint32_t regno)
For most registers, returns whether they exist or not.
Definition: riscv_reg.c:348
static bool reg_exists(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:329
static int init_custom_register_names(struct list_head *expose_custom, struct reg_name_table *custom_register_names)
Definition: riscv_reg.c:620
struct target * riscv_reg_impl_get_target(const struct reg *reg)
Return the target that owns the cache entry.
Definition: riscv_reg.c:187
static unsigned int gdb_regno_custom_number(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:528
int riscv_reg_set(struct target *target, enum gdb_regno regid, riscv_reg_t value)
This function is used to change the value of a register.
Definition: riscv_reg.c:900
void riscv_reg_cache_invalidate_all(struct target *target)
Invalidate all registers - forget their cached register values.
Definition: riscv_reg.c:881
static void init_custom_csr_names(const struct target *target)
Definition: riscv_reg.c:126
const char * riscv_reg_gdb_regno_name(const struct target *target, enum gdb_regno regno)
This file describes the register cache interface available to the RISC-V target.
Definition: riscv_reg.c:145
int riscv_reg_impl_expose_csrs(const struct target *target)
Expose additional CSRs, as specified by riscv_info_t::expose_csr list.
Definition: riscv_reg.c:691
int riscv_reg_impl_init_cache_entry(struct target *target, uint32_t regno, bool exist, const struct reg_arch_type *reg_type)
Initialize register.
Definition: riscv_reg.c:588
static void free_reg_names(struct target *target)
Definition: riscv_reg.c:111
int riscv_reg_impl_init_cache(struct target *target)
Initialize register cache.
Definition: riscv_reg.c:660
static struct reg_data_type * gdb_regno_reg_data_type(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:242
static bool vlenb_exists(const struct target *target)
Definition: riscv_reg.c:324
uint32_t gdb_regno_size(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:297
int riscv_reg_flush_all(struct target *target)
Write all dirty registers to the target.
Definition: riscv_reg.c:758
static bool is_known_standard_csr(unsigned int csr_num)
Definition: riscv_reg.c:336
static const char * gdb_regno_group(uint32_t regno)
Definition: riscv_reg.c:278
int riscv_reg_get(struct target *target, riscv_reg_t *value, enum gdb_regno regid)
This function is used to get the value of a register.
Definition: riscv_reg.c:934
static const char *const default_reg_names[GDB_REGNO_COUNT]
TODO: Currently reg->get/set is implemented in terms of riscv_get/set_register.
Definition: riscv_reg.c:25
struct reg * riscv_reg_impl_cache_entry(const struct target *target, uint32_t number)
Return the entry in the register cache of the target.
Definition: riscv_reg.c:551
static void free_custom_register_names(struct target *target)
Definition: riscv_reg.c:98
int riscv_reg_write(struct target *target, enum gdb_regno regid, riscv_reg_t value)
This function is used to change the value of a register.
Definition: riscv_reg.c:917
void riscv_reg_free_all(struct target *target)
Free register cache and associated structures.
Definition: riscv_reg.c:741
bool riscv_reg_cache_any_dirty(const struct target *target, int log_level)
Check whether there are any dirty registers in the OpenOCD's register cache.
Definition: riscv_reg.c:862
static bool gdb_regno_caller_save(uint32_t regno)
Definition: riscv_reg.c:235
static bool riscv_reg_impl_is_initialized(const struct reg *reg)
This file describes the helpers to use during register cache initialization of a RISC-V target.
static bool riscv_reg_impl_gdb_regno_cacheable(enum gdb_regno regno, bool is_write)
If write is true: return true iff we are guaranteed that the register will contain exactly the value ...
struct target * target
Definition: rtt/rtt.c:26
Definition: list.h:41
char * name
Definition: riscv.h:122
uint16_t high
Definition: riscv.h:121
uint16_t low
Definition: riscv.h:121
const char * name
Definition: register.h:145
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_data_type_union_field * fields
Definition: register.h:57
enum reg_type type
Definition: register.h:100
const char * name
Definition: register.h:42
char ** reg_names
Definition: riscv.h:133
unsigned int num_entries
Definition: riscv.h:132
Definition: register.h:111
bool caller_save
Definition: register.h:119
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
const char * group
Definition: register.h:138
uint8_t * value
Definition: register.h:122
struct reg_feature * feature
Definition: register.h:117
struct reg_data_type * reg_data_type
Definition: register.h:135
uint32_t number
Definition: register.h:115
bool hidden
Definition: register.h:130
void * arch_info
Definition: register.h:140
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
struct target * target
Definition: riscv.h:97
unsigned int custom_number
Definition: riscv.h:99
Definition: target.h:119
enum target_state state
Definition: target.h:167
struct reg_cache * reg_cache
Definition: target.h:168
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:247
@ TARGET_HALTED
Definition: target.h:58
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t offset[4]
Definition: vdebug.c:9