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 char *init_reg_name(const char *name)
127 {
128  const int size_buf = strlen(name) + 1;
129 
130  char * const buf = calloc(size_buf, sizeof(char));
131  if (!buf) {
132  LOG_ERROR("Failed to allocate memory for a register name.");
133  return NULL;
134  }
135  strcpy(buf, name);
136  return buf;
137 }
138 
139 static void init_custom_csr_names(const struct target *target)
140 {
141  RISCV_INFO(info);
142  range_list_t *entry;
143 
144  list_for_each_entry(entry, &info->expose_csr, list) {
145  if (!entry->name)
146  continue;
147  assert(entry->low == entry->high);
148  const unsigned int regno = entry->low + GDB_REGNO_CSR0;
149  assert(regno <= GDB_REGNO_CSR4095);
150  if (info->reg_names[regno])
151  return;
152  info->reg_names[regno] = init_reg_name(entry->name);
153  }
154 }
155 
156 static char *init_reg_name_with_prefix(const char *name_prefix,
157  unsigned int num)
158 {
159  const int size_buf = snprintf(NULL, 0, "%s%d", name_prefix, num) + 1;
160 
161  char * const buf = calloc(size_buf, sizeof(char));
162  if (!buf) {
163  LOG_ERROR("Failed to allocate memory for a register name.");
164  return NULL;
165  }
166  int result = snprintf(buf, size_buf, "%s%d", name_prefix, num);
167  assert(result > 0 && result <= (size_buf - 1));
168  return buf;
169 }
170 
171 const char *riscv_reg_gdb_regno_name(const struct target *target, enum gdb_regno regno)
172 {
173  RISCV_INFO(info);
174 
175  if (regno >= GDB_REGNO_COUNT) {
176  assert(info->custom_register_names.reg_names);
177  assert(regno - GDB_REGNO_COUNT <= info->custom_register_names.num_entries);
178  return info->custom_register_names.reg_names[regno - GDB_REGNO_COUNT];
179  }
180 
181  if (!info->reg_names)
182  info->reg_names = calloc(GDB_REGNO_COUNT, sizeof(char *));
183 
184  if (info->reg_names[regno])
185  return info->reg_names[regno];
186  if (default_reg_names[regno])
187  return default_reg_names[regno];
188  if (regno <= GDB_REGNO_XPR31) {
189  info->reg_names[regno] = init_reg_name_with_prefix("x", regno - GDB_REGNO_ZERO);
190  return info->reg_names[regno];
191  }
192  if (regno <= GDB_REGNO_V31 && regno >= GDB_REGNO_V0) {
193  info->reg_names[regno] = init_reg_name_with_prefix("v", regno - GDB_REGNO_V0);
194  return info->reg_names[regno];
195  }
196  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095) {
198  if (!info->reg_names[regno])
199  info->reg_names[regno] = init_reg_name_with_prefix("csr", regno - GDB_REGNO_CSR0);
200  return info->reg_names[regno];
201  }
202  assert(!"Encountered uninitialized entry in reg_names table");
203 
204  return NULL;
205 }
206 
207 struct target *riscv_reg_impl_get_target(const struct reg *reg)
208 {
210  return ((const riscv_reg_info_t *)reg->arch_info)->target;
211 }
212 
213 static struct reg_feature *gdb_regno_feature(uint32_t regno)
214 {
215  if (regno <= GDB_REGNO_XPR31 || regno == GDB_REGNO_PC) {
216  static struct reg_feature feature_cpu = {
217  .name = "org.gnu.gdb.riscv.cpu"
218  };
219  return &feature_cpu;
220  }
221  if ((regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) ||
222  regno == GDB_REGNO_FFLAGS ||
223  regno == GDB_REGNO_FRM ||
224  regno == GDB_REGNO_FCSR) {
225  static struct reg_feature feature_fpu = {
226  .name = "org.gnu.gdb.riscv.fpu"
227  };
228  return &feature_fpu;
229  }
230  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31) {
231  static struct reg_feature feature_vector = {
232  .name = "org.gnu.gdb.riscv.vector"
233  };
234  return &feature_vector;
235  }
236  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095) {
237  static struct reg_feature feature_csr = {
238  .name = "org.gnu.gdb.riscv.csr"
239  };
240  return &feature_csr;
241  }
242  if (regno == GDB_REGNO_PRIV) {
243  static struct reg_feature feature_virtual = {
244  .name = "org.gnu.gdb.riscv.virtual"
245  };
246  return &feature_virtual;
247  }
248  assert(regno >= GDB_REGNO_COUNT);
249  static struct reg_feature feature_custom = {
250  .name = "org.gnu.gdb.riscv.custom"
251  };
252  return &feature_custom;
253 }
254 
255 static bool gdb_regno_caller_save(uint32_t regno)
256 {
257  return regno <= GDB_REGNO_XPR31 ||
258  regno == GDB_REGNO_PC ||
259  (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31);
260 }
261 
262 static struct reg_data_type *gdb_regno_reg_data_type(const struct target *target,
263  uint32_t regno)
264 {
265  if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) {
266  static struct reg_data_type type_ieee_single = {
268  .id = "ieee_single"
269  };
270  static struct reg_data_type type_ieee_double = {
272  .id = "ieee_double"
273  };
274  static struct reg_data_type_union_field single_double_fields[] = {
275  {"float", &type_ieee_single, single_double_fields + 1},
276  {"double", &type_ieee_double, NULL},
277  };
278  static struct reg_data_type_union single_double_union = {
279  .fields = single_double_fields
280  };
281  static struct reg_data_type type_ieee_single_double = {
283  .id = "FPU_FD",
284  .type_class = REG_TYPE_CLASS_UNION,
285  {.reg_type_union = &single_double_union}
286  };
287  return riscv_supports_extension(target, 'D') ?
288  &type_ieee_single_double :
289  &type_ieee_single;
290  }
291  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31) {
292  RISCV_INFO(info);
293  return &info->type_vector;
294  }
295  return NULL;
296 }
297 
298 static const char *gdb_regno_group(uint32_t regno)
299 {
300  if (regno <= GDB_REGNO_XPR31 ||
301  regno == GDB_REGNO_PC ||
302  regno == GDB_REGNO_PRIV)
303  return "general";
304  if ((regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31) ||
305  regno == GDB_REGNO_FFLAGS ||
306  regno == GDB_REGNO_FRM ||
307  regno == GDB_REGNO_FCSR)
308  return "float";
309  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095)
310  return "csr";
311  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31)
312  return "vector";
313  assert(regno >= GDB_REGNO_COUNT);
314  return "custom";
315 }
316 
317 uint32_t gdb_regno_size(const struct target *target, uint32_t regno)
318 {
319  if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
320  return riscv_supports_extension(target, 'D') ? 64 : 32;
321  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31)
322  return riscv_vlenb(target) * 8;
323  if (regno == GDB_REGNO_PRIV)
324  return 8;
325  if (regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095) {
326  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
327  switch (csr_number) {
328  case CSR_DCSR:
329  case CSR_MVENDORID:
330  case CSR_MCOUNTINHIBIT:
331 
332  case CSR_FFLAGS:
333  case CSR_FRM:
334  case CSR_FCSR:
335 
336  case CSR_SCOUNTEREN:
337  case CSR_MCOUNTEREN:
338  return 32;
339  }
340  }
341  return riscv_xlen(target);
342 }
343 
344 static bool vlenb_exists(const struct target *target)
345 {
346  return riscv_vlenb(target) != 0;
347 }
348 
349 static bool reg_exists(const struct target *target, uint32_t regno)
350 {
351  const struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
353  return reg->exist;
354 }
355 
356 static bool is_known_standard_csr(unsigned int csr_num)
357 {
358  static const bool is_csr_in_buf[GDB_REGNO_CSR4095 - GDB_REGNO_CSR0 + 1] = {
359  #define DECLARE_CSR(csr_name, number)[number] = true,
360  #include "encoding.h"
361  #undef DECLARE_CSR
362  };
363  assert(csr_num < ARRAY_SIZE(is_csr_in_buf));
364 
365  return is_csr_in_buf[csr_num];
366 }
367 
368 bool riscv_reg_impl_gdb_regno_exist(const struct target *target, uint32_t regno)
369 {
370  switch (regno) {
371  case GDB_REGNO_VLENB:
372  case GDB_REGNO_MTOPI:
373  case GDB_REGNO_MTOPEI:
374  assert(false
375  && "Existence of other registers is determined "
376  "depending on existence of these ones, so "
377  "whether these register exist or not should be "
378  "set explicitly.");
379  };
380 
381  if (regno <= GDB_REGNO_XPR15 ||
382  regno == GDB_REGNO_PC ||
383  regno == GDB_REGNO_PRIV)
384  return true;
385  if (regno > GDB_REGNO_XPR15 && regno <= GDB_REGNO_XPR31)
386  return !riscv_supports_extension(target, 'E');
387  if (regno >= GDB_REGNO_FPR0 && regno <= GDB_REGNO_FPR31)
388  return riscv_supports_extension(target, 'F');
389  if (regno >= GDB_REGNO_V0 && regno <= GDB_REGNO_V31)
390  return vlenb_exists(target);
391  if (regno >= GDB_REGNO_COUNT)
392  return true;
393  assert(regno >= GDB_REGNO_CSR0 && regno <= GDB_REGNO_CSR4095);
394  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
395  switch (csr_number) {
396  case CSR_FFLAGS:
397  case CSR_FRM:
398  case CSR_FCSR:
399  return riscv_supports_extension(target, 'F');
400  case CSR_VSTART:
401  case CSR_VXSAT:
402  case CSR_VXRM:
403  case CSR_VL:
404  case CSR_VCSR:
405  case CSR_VTYPE:
406  return vlenb_exists(target);
407  case CSR_SCOUNTEREN:
408  case CSR_SSTATUS:
409  case CSR_STVEC:
410  case CSR_SIP:
411  case CSR_SIE:
412  case CSR_SSCRATCH:
413  case CSR_SEPC:
414  case CSR_SCAUSE:
415  case CSR_STVAL:
416  case CSR_SATP:
417  return riscv_supports_extension(target, 'S');
418  case CSR_MEDELEG:
419  case CSR_MIDELEG:
420  /* "In systems with only M-mode, or with both M-mode and
421  * U-mode but without U-mode trap support, the medeleg and
422  * mideleg registers should not exist." */
423  return riscv_supports_extension(target, 'S') ||
425 
426  case CSR_PMPCFG1:
427  case CSR_PMPCFG3:
428  case CSR_CYCLEH:
429  case CSR_TIMEH:
430  case CSR_INSTRETH:
431  case CSR_HPMCOUNTER3H:
432  case CSR_HPMCOUNTER4H:
433  case CSR_HPMCOUNTER5H:
434  case CSR_HPMCOUNTER6H:
435  case CSR_HPMCOUNTER7H:
436  case CSR_HPMCOUNTER8H:
437  case CSR_HPMCOUNTER9H:
438  case CSR_HPMCOUNTER10H:
439  case CSR_HPMCOUNTER11H:
440  case CSR_HPMCOUNTER12H:
441  case CSR_HPMCOUNTER13H:
442  case CSR_HPMCOUNTER14H:
443  case CSR_HPMCOUNTER15H:
444  case CSR_HPMCOUNTER16H:
445  case CSR_HPMCOUNTER17H:
446  case CSR_HPMCOUNTER18H:
447  case CSR_HPMCOUNTER19H:
448  case CSR_HPMCOUNTER20H:
449  case CSR_HPMCOUNTER21H:
450  case CSR_HPMCOUNTER22H:
451  case CSR_HPMCOUNTER23H:
452  case CSR_HPMCOUNTER24H:
453  case CSR_HPMCOUNTER25H:
454  case CSR_HPMCOUNTER26H:
455  case CSR_HPMCOUNTER27H:
456  case CSR_HPMCOUNTER28H:
457  case CSR_HPMCOUNTER29H:
458  case CSR_HPMCOUNTER30H:
459  case CSR_HPMCOUNTER31H:
460  case CSR_MCYCLEH:
461  case CSR_MINSTRETH:
462  case CSR_MHPMCOUNTER4H:
463  case CSR_MHPMCOUNTER5H:
464  case CSR_MHPMCOUNTER6H:
465  case CSR_MHPMCOUNTER7H:
466  case CSR_MHPMCOUNTER8H:
467  case CSR_MHPMCOUNTER9H:
468  case CSR_MHPMCOUNTER10H:
469  case CSR_MHPMCOUNTER11H:
470  case CSR_MHPMCOUNTER12H:
471  case CSR_MHPMCOUNTER13H:
472  case CSR_MHPMCOUNTER14H:
473  case CSR_MHPMCOUNTER15H:
474  case CSR_MHPMCOUNTER16H:
475  case CSR_MHPMCOUNTER17H:
476  case CSR_MHPMCOUNTER18H:
477  case CSR_MHPMCOUNTER19H:
478  case CSR_MHPMCOUNTER20H:
479  case CSR_MHPMCOUNTER21H:
480  case CSR_MHPMCOUNTER22H:
481  case CSR_MHPMCOUNTER23H:
482  case CSR_MHPMCOUNTER24H:
483  case CSR_MHPMCOUNTER25H:
484  case CSR_MHPMCOUNTER26H:
485  case CSR_MHPMCOUNTER27H:
486  case CSR_MHPMCOUNTER28H:
487  case CSR_MHPMCOUNTER29H:
488  case CSR_MHPMCOUNTER30H:
489  case CSR_MHPMCOUNTER31H:
490  return riscv_xlen(target) == 32;
491  case CSR_MCOUNTEREN:
492  return riscv_supports_extension(target, 'U');
493  /* Interrupts M-Mode CSRs. */
494  case CSR_MISELECT:
495  case CSR_MIREG:
496  case CSR_MVIEN:
497  case CSR_MVIP:
498  case CSR_MIEH:
499  case CSR_MIPH:
501  case CSR_MIDELEGH:
502  case CSR_MVIENH:
503  case CSR_MVIPH:
504  return reg_exists(target, GDB_REGNO_MTOPI) &&
505  riscv_xlen(target) == 32 &&
507  /* Interrupts S-Mode CSRs. */
508  case CSR_SISELECT:
509  case CSR_SIREG:
510  case CSR_STOPI:
511  return reg_exists(target, GDB_REGNO_MTOPI) &&
513  case CSR_STOPEI:
516  case CSR_SIEH:
517  case CSR_SIPH:
518  return reg_exists(target, GDB_REGNO_MTOPI) &&
519  riscv_xlen(target) == 32 &&
521  /* Interrupts Hypervisor and VS CSRs. */
522  case CSR_HVIEN:
523  case CSR_HVICTL:
524  case CSR_HVIPRIO1:
525  case CSR_HVIPRIO2:
526  case CSR_VSISELECT:
527  case CSR_VSIREG:
528  case CSR_VSTOPI:
529  return reg_exists(target, GDB_REGNO_MTOPI) &&
531  case CSR_VSTOPEI:
534  case CSR_HIDELEGH:
535  case CSR_HVIENH:
536  case CSR_HVIPH:
537  case CSR_HVIPRIO1H:
538  case CSR_HVIPRIO2H:
539  case CSR_VSIEH:
540  case CSR_VSIPH:
541  return reg_exists(target, GDB_REGNO_MTOPI) &&
542  riscv_xlen(target) == 32 &&
544  }
545  return is_known_standard_csr(csr_number);
546 }
547 
548 static unsigned int gdb_regno_custom_number(const struct target *target, uint32_t regno)
549 {
550  if (regno < GDB_REGNO_COUNT)
551  return 0;
552 
553  RISCV_INFO(info);
554  assert(!list_empty(&info->expose_custom));
556  unsigned int regno_start = GDB_REGNO_COUNT;
557  unsigned int start = 0;
558  unsigned int offset = 0;
559  list_for_each_entry(range, &info->expose_custom, list) {
560  start = range->low;
561  assert(regno >= regno_start);
562  offset = regno - regno_start;
563  const unsigned int regs_in_range = range->high - range->low + 1;
564  if (offset < regs_in_range)
565  break;
566  regno_start += regs_in_range;
567  }
568  return start + offset;
569 }
570 
572  uint32_t number)
573 {
574  assert(target->reg_cache);
575  assert(target->reg_cache->reg_list);
576  assert(number < target->reg_cache->num_regs);
577  return &target->reg_cache->reg_list[number];
578 }
579 
580 static int resize_reg(const struct target *target, uint32_t regno, bool exist,
581  uint32_t size)
582 {
583  struct reg *reg = riscv_reg_impl_cache_entry(target, regno);
585  free(reg->value);
586  reg->size = size;
587  reg->exist = exist;
588  if (reg->exist) {
589  reg->value = malloc(DIV_ROUND_UP(reg->size, 8));
590  if (!reg->value) {
591  LOG_ERROR("Failed to allocate memory.");
592  return ERROR_FAIL;
593  }
594  } else {
595  reg->value = NULL;
596  }
598  return ERROR_OK;
599 }
600 
601 int riscv_reg_impl_set_exist(const struct target *target, uint32_t regno, bool exist)
602 {
603  const struct reg *reg = riscv_reg_impl_cache_entry(target, regno);
605  return resize_reg(target, regno, exist, reg->size);
606 }
607 
608 int riscv_reg_impl_init_cache_entry(struct target *target, uint32_t regno,
609  bool exist, const struct reg_arch_type *reg_type)
610 {
611  struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
613  return ERROR_OK;
614  reg->number = regno;
615  reg->type = reg_type;
616  reg->dirty = false;
617  reg->valid = false;
618  reg->hidden = false;
620  reg->feature = gdb_regno_feature(regno);
623  reg->group = gdb_regno_group(regno);
624  if (regno < GDB_REGNO_COUNT) {
625  RISCV_INFO(info);
626  reg->arch_info = &info->shared_reg_info;
627  } else {
628  reg->arch_info = calloc(1, sizeof(riscv_reg_info_t));
629  if (!reg->arch_info) {
630  LOG_ERROR("Out of memory.");
631  return ERROR_FAIL;
632  }
633  riscv_reg_info_t * const reg_arch_info = reg->arch_info;
634  reg_arch_info->target = target;
635  reg_arch_info->custom_number = gdb_regno_custom_number(target, regno);
636  }
637  return resize_reg(target, regno, exist, gdb_regno_size(target, regno));
638 }
639 
640 static int init_custom_register_names(struct list_head *expose_custom,
641  struct reg_name_table *custom_register_names)
642 {
643  unsigned int custom_regs_num = 0;
644  if (!list_empty(expose_custom)) {
645  range_list_t *entry;
646  list_for_each_entry(entry, expose_custom, list)
647  custom_regs_num += entry->high - entry->low + 1;
648  }
649 
650  if (!custom_regs_num)
651  return ERROR_OK;
652 
653  custom_register_names->reg_names = calloc(custom_regs_num, sizeof(char *));
654  if (!custom_register_names->reg_names) {
655  LOG_ERROR("Failed to allocate memory for custom_register_names->reg_names");
656  return ERROR_FAIL;
657  }
658  custom_register_names->num_entries = custom_regs_num;
659  char **reg_names = custom_register_names->reg_names;
661  unsigned int next_custom_reg_index = 0;
662  list_for_each_entry(range, expose_custom, list) {
663  for (unsigned int custom_number = range->low; custom_number <= range->high; ++custom_number) {
664  if (range->name)
665  reg_names[next_custom_reg_index] = init_reg_name(range->name);
666  else
667  reg_names[next_custom_reg_index] =
668  init_reg_name_with_prefix("custom", custom_number);
669 
670  if (!reg_names[next_custom_reg_index])
671  return ERROR_FAIL;
672  ++next_custom_reg_index;
673  }
674  }
675  return ERROR_OK;
676 }
677 
679 {
680  RISCV_INFO(info);
681 
683 
684  target->reg_cache = calloc(1, sizeof(*target->reg_cache));
685  if (!target->reg_cache) {
686  LOG_TARGET_ERROR(target, "Failed to allocate memory for target->reg_cache");
687  return ERROR_FAIL;
688  }
689  target->reg_cache->name = "RISC-V Registers";
690 
691  if (init_custom_register_names(&info->expose_custom, &info->custom_register_names) != ERROR_OK) {
692  LOG_TARGET_ERROR(target, "init_custom_register_names failed");
693  return ERROR_FAIL;
694  }
695 
696  target->reg_cache->num_regs = GDB_REGNO_COUNT + info->custom_register_names.num_entries;
697  LOG_TARGET_DEBUG(target, "create register cache for %d registers",
699 
701  calloc(target->reg_cache->num_regs, sizeof(struct reg));
702  if (!target->reg_cache->reg_list) {
703  LOG_TARGET_ERROR(target, "Failed to allocate memory for target->reg_cache->reg_list");
704  return ERROR_FAIL;
705  }
706  return ERROR_OK;
707 }
708 
710 {
711  RISCV_INFO(info);
712  range_list_t *entry;
713  list_for_each_entry(entry, &info->expose_csr, list) {
714  assert(entry->low <= entry->high);
715  assert(entry->high <= GDB_REGNO_CSR4095 - GDB_REGNO_CSR0);
716  const enum gdb_regno last_regno = GDB_REGNO_CSR0 + entry->high;
717  for (enum gdb_regno regno = GDB_REGNO_CSR0 + entry->low;
718  regno <= last_regno; ++regno) {
719  struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
720  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
721  if (reg->exist) {
723  "Not exposing CSR %d: register already exists.",
724  csr_number);
725  continue;
726  }
727  if (riscv_reg_impl_set_exist(target, regno, /*exist*/ true) != ERROR_OK)
728  return ERROR_FAIL;
729  LOG_TARGET_DEBUG(target, "Exposing additional CSR %d (name=%s)",
730  csr_number, reg->name);
731  }
732  }
733  return ERROR_OK;
734 }
735 
737 {
738  RISCV_INFO(info);
739  range_list_t *entry;
740  list_for_each_entry(entry, &info->hide_csr, list) {
741  assert(entry->high <= GDB_REGNO_CSR4095 - GDB_REGNO_CSR0);
742  const enum gdb_regno last_regno = GDB_REGNO_CSR0 + entry->high;
743  for (enum gdb_regno regno = GDB_REGNO_CSR0 + entry->low;
744  regno <= last_regno; ++regno) {
745  struct reg * const reg = riscv_reg_impl_cache_entry(target, regno);
746  const unsigned int csr_number = regno - GDB_REGNO_CSR0;
747  if (!reg->exist) {
749  "Not hiding CSR %d: register does not exist.",
750  csr_number);
751  continue;
752  }
753  LOG_TARGET_DEBUG(target, "Hiding CSR %d (name=%s).", csr_number, reg->name);
754  reg->hidden = true;
755  }
756  }
757 }
758 
760 {
762  /* Free the shared structure use for most registers. */
763  if (!target->reg_cache)
764  return;
765  if (target->reg_cache->reg_list) {
766  for (unsigned int i = GDB_REGNO_COUNT; i < target->reg_cache->num_regs; i++)
767  free(target->reg_cache->reg_list[i].arch_info);
768  for (unsigned int i = 0; i < target->reg_cache->num_regs; i++)
769  free(target->reg_cache->reg_list[i].value);
770  free(target->reg_cache->reg_list);
771  }
772  free(target->reg_cache);
773  target->reg_cache = NULL;
774 }
775 
777 {
778  if (!target->reg_cache)
779  return ERROR_OK;
780 
781  LOG_TARGET_DEBUG(target, "Flushing register cache");
782 
783  /* Writing non-GPR registers may require progbuf execution, and some GPRs
784  * may become dirty in the process (e.g. S0, S1). For that reason, flush
785  * registers in reverse order, so that GPRs are flushed last.
786  */
787  for (unsigned int number = target->reg_cache->num_regs; number-- > 0; ) {
789  if (reg->valid && reg->dirty) {
791 
792  LOG_TARGET_DEBUG(target, "%s is dirty; write back 0x%" PRIx64,
793  reg->name, value);
795  return ERROR_FAIL;
796  }
797  }
798  LOG_TARGET_DEBUG(target, "Flush of register cache completed");
799  return ERROR_OK;
800 }
801 
815  enum gdb_regno regid, riscv_reg_t value, bool write_through)
816 {
817  RISCV_INFO(r);
818  assert(r);
819  if (r->dtm_version == DTM_DTMCS_VERSION_0_11)
820  return riscv011_set_register(target, regid, value);
821 
822  keep_alive();
823 
824  if (regid == GDB_REGNO_PC) {
825  return riscv_set_or_write_register(target, GDB_REGNO_DPC, value, write_through);
826  } else if (regid == GDB_REGNO_PRIV) {
827  riscv_reg_t dcsr;
828 
830  return ERROR_FAIL;
833  return riscv_set_or_write_register(target, GDB_REGNO_DCSR, dcsr, write_through);
834  }
835 
836  struct reg *reg = riscv_reg_impl_cache_entry(target, regid);
838 
839  if (!reg->exist) {
840  LOG_TARGET_DEBUG(target, "Register %s does not exist.", reg->name);
841  return ERROR_FAIL;
842  }
843 
844  if (target->state != TARGET_HALTED) {
846  "Target not halted, writing to target: %s <- 0x%" PRIx64,
847  reg->name, value);
848  return riscv013_set_register(target, regid, value);
849  }
850 
851  const bool need_to_write = !reg->valid || reg->dirty ||
852  value != buf_get_u64(reg->value, 0, reg->size);
853  const bool cacheable = riscv_reg_impl_gdb_regno_cacheable(regid, need_to_write);
854 
855  if (!cacheable || (write_through && need_to_write)) {
857  "Writing to target: %s <- 0x%" PRIx64 " (cacheable=%s, valid=%s, dirty=%s)",
858  reg->name, value, cacheable ? "true" : "false",
859  reg->valid ? "true" : "false",
860  reg->dirty ? "true" : "false");
861  if (riscv013_set_register(target, regid, value) != ERROR_OK)
862  return ERROR_FAIL;
863 
864  reg->dirty = false;
865  } else {
866  reg->dirty = need_to_write;
867  }
868 
869  buf_set_u64(reg->value, 0, reg->size, value);
870  reg->valid = cacheable;
871 
873  "Wrote 0x%" PRIx64 " to %s (cacheable=%s, valid=%s, dirty=%s)",
874  value, reg->name, cacheable ? "true" : "false",
875  reg->valid ? "true" : "false",
876  reg->dirty ? "true" : "false");
877  return ERROR_OK;
878 }
879 
880 bool riscv_reg_cache_any_dirty(const struct target *target, int log_level)
881 {
882  bool any_dirty = false;
883 
884  if (!target->reg_cache)
885  return any_dirty;
886 
887  for (unsigned int number = 0; number < target->reg_cache->num_regs; ++number) {
888  const struct reg * const reg = riscv_reg_impl_cache_entry(target, number);
890  if (reg->dirty) {
891  log_printf_lf(log_level, __FILE__, __LINE__, __func__,
892  "[%s] Register %s is dirty!", target_name(target), reg->name);
893  any_dirty = true;
894  }
895  }
896  return any_dirty;
897 }
898 
900 {
901  if (!target->reg_cache)
902  return;
903 
904  LOG_TARGET_DEBUG(target, "Invalidating register cache.");
906 }
907 
918 int riscv_reg_set(struct target *target, enum gdb_regno regid,
920 {
922  /* write_through */ false);
923 }
924 
935 int riscv_reg_write(struct target *target, enum gdb_regno regid,
937 {
939  /* write_through */ true);
940 }
941 
953  enum gdb_regno regid)
954 {
955  RISCV_INFO(r);
956  assert(r);
957  if (r->dtm_version == DTM_DTMCS_VERSION_0_11)
958  return riscv011_get_register(target, value, regid);
959 
960  keep_alive();
961 
962  if (regid == GDB_REGNO_PC)
964 
965  struct reg *reg = riscv_reg_impl_cache_entry(target, regid);
967  if (!reg->exist) {
968  LOG_TARGET_DEBUG(target, "Register %s does not exist.", reg->name);
969  return ERROR_FAIL;
970  }
971 
972  if (reg->valid) {
973  *value = buf_get_u64(reg->value, 0, reg->size);
974  LOG_TARGET_DEBUG(target, "Read %s: 0x%" PRIx64 " (cached)", reg->name,
975  *value);
976  return ERROR_OK;
977  }
978 
979  LOG_TARGET_DEBUG(target, "Reading %s from target", reg->name);
980  if (riscv013_get_register(target, value, regid) != ERROR_OK)
981  return ERROR_FAIL;
982 
983  buf_set_u64(reg->value, 0, reg->size, *value);
984  reg->valid = riscv_reg_impl_gdb_regno_cacheable(regid, /* is write? */ false) &&
986  reg->dirty = false;
987 
988  LOG_TARGET_DEBUG(target, "Read %s: 0x%" PRIx64, reg->name, *value);
989  return ERROR_OK;
990 }
const char * name
Definition: armv4_5.c:76
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:109
@ 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:110
@ 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:117
@ 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:124
@ 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:112
@ GDB_REGNO_S2
Definition: gdb_regs.h:31
@ GDB_REGNO_CSR4095
Definition: gdb_regs.h:111
@ 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:125
@ 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:194
void keep_alive(void)
Definition: log.c:429
static int64_t start
Definition: log.c:54
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:160
#define ERROR_FAIL
Definition: log.h:175
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:163
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:151
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define ERROR_OK
Definition: log.h:169
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:1344
int riscv011_set_register(struct target *target, enum gdb_regno regid, riscv_reg_t value)
Definition: riscv-011.c:1392
int riscv013_set_register(struct target *target, enum gdb_regno rid, riscv_reg_t value)
Definition: riscv-013.c:5139
int riscv013_get_register(struct target *target, riscv_reg_t *value, enum gdb_regno rid)
Definition: riscv-013.c:5110
unsigned int riscv_xlen(const struct target *target)
Definition: riscv.c:6060
bool riscv_supports_extension(const struct target *target, char letter)
Definition: riscv.c:6047
unsigned int riscv_vlenb(const struct target *target)
Definition: riscv.c:6066
#define RISCV_INFO(R)
Definition: riscv.h:426
uint64_t riscv_reg_t
Definition: riscv.h:46
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:601
static struct reg_feature * gdb_regno_feature(uint32_t regno)
Definition: riscv_reg.c:213
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:814
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:736
static int resize_reg(const struct target *target, uint32_t regno, bool exist, uint32_t size)
Definition: riscv_reg.c:580
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:368
static bool reg_exists(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:349
static char * init_reg_name(const char *name)
Definition: riscv_reg.c:126
static int init_custom_register_names(struct list_head *expose_custom, struct reg_name_table *custom_register_names)
Definition: riscv_reg.c:640
struct target * riscv_reg_impl_get_target(const struct reg *reg)
Return the target that owns the cache entry.
Definition: riscv_reg.c:207
static unsigned int gdb_regno_custom_number(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:548
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:918
void riscv_reg_cache_invalidate_all(struct target *target)
Invalidate all registers - forget their cached register values.
Definition: riscv_reg.c:899
static void init_custom_csr_names(const struct target *target)
Definition: riscv_reg.c:139
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:171
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:709
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:608
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:678
static struct reg_data_type * gdb_regno_reg_data_type(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:262
static bool vlenb_exists(const struct target *target)
Definition: riscv_reg.c:344
uint32_t gdb_regno_size(const struct target *target, uint32_t regno)
Definition: riscv_reg.c:317
int riscv_reg_flush_all(struct target *target)
Write all dirty registers to the target.
Definition: riscv_reg.c:776
static bool is_known_standard_csr(unsigned int csr_num)
Definition: riscv_reg.c:356
static const char * gdb_regno_group(uint32_t regno)
Definition: riscv_reg.c:298
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:952
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:571
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:935
void riscv_reg_free_all(struct target *target)
Free register cache and associated structures.
Definition: riscv_reg.c:759
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:880
static char * init_reg_name_with_prefix(const char *name_prefix, unsigned int num)
Definition: riscv_reg.c:156
static bool gdb_regno_caller_save(uint32_t regno)
Definition: riscv_reg.c:255
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:160
struct reg_cache * reg_cache
Definition: target.h:161
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:236
@ 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