1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2024 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This must come before any other includes. */
31 #include "sim-assert.h"
33 /* property entries */
35 struct hw_property_data
37 struct hw_property_data
*next
;
38 struct hw_property
*property
;
39 const void *init_array
;
40 unsigned sizeof_init_array
;
44 create_hw_property_data (struct hw
*me
)
49 delete_hw_property_data (struct hw
*me
)
54 /* Device Properties: */
56 static struct hw_property_data
*
57 find_property_data (struct hw
*me
,
60 struct hw_property_data
*entry
;
61 ASSERT (property
!= NULL
);
62 entry
= me
->properties_of_hw
;
65 if (strcmp (entry
->property
->name
, property
) == 0)
74 hw_add_property (struct hw
*me
,
76 hw_property_type type
,
77 const void *init_array
,
78 unsigned sizeof_init_array
,
80 unsigned sizeof_array
,
81 const struct hw_property
*original
,
82 object_disposition disposition
)
84 struct hw_property_data
*new_entry
= NULL
;
85 struct hw_property
*new_value
= NULL
;
87 /* find the list end */
88 struct hw_property_data
**insertion_point
= &me
->properties_of_hw
;
89 while (*insertion_point
!= NULL
)
91 if (strcmp ((*insertion_point
)->property
->name
, property
) == 0)
93 insertion_point
= &(*insertion_point
)->next
;
96 /* create a new value */
97 new_value
= HW_ZALLOC (me
, struct hw_property
);
98 new_value
->name
= (char *) strdup (property
);
99 new_value
->type
= type
;
100 if (sizeof_array
> 0)
102 void *new_array
= hw_zalloc (me
, sizeof_array
);
103 memcpy (new_array
, array
, sizeof_array
);
104 new_value
->array
= new_array
;
105 new_value
->sizeof_array
= sizeof_array
;
107 new_value
->owner
= me
;
108 new_value
->original
= original
;
109 new_value
->disposition
= disposition
;
111 /* insert the value into the list */
112 new_entry
= HW_ZALLOC (me
, struct hw_property_data
);
113 *insertion_point
= new_entry
;
114 if (sizeof_init_array
> 0)
116 void *new_init_array
= hw_zalloc (me
, sizeof_init_array
);
117 memcpy (new_init_array
, init_array
, sizeof_init_array
);
118 new_entry
->init_array
= new_init_array
;
119 new_entry
->sizeof_init_array
= sizeof_init_array
;
121 new_entry
->property
= new_value
;
126 hw_set_property (struct hw
*me
,
127 const char *property
,
128 hw_property_type type
,
132 /* find the property */
133 struct hw_property_data
*entry
= find_property_data (me
, property
);
136 /* existing property - update it */
138 struct hw_property
*value
= entry
->property
;
139 /* check the type matches */
140 if (value
->type
!= type
)
141 hw_abort (me
, "conflict between type of new and old value for property %s", property
);
142 /* replace its value */
143 if (value
->array
!= NULL
)
144 hw_free (me
, (void*)value
->array
);
145 new_array
= (sizeof_array
> 0
146 ? hw_zalloc (me
, sizeof_array
)
148 value
->array
= new_array
;
149 value
->sizeof_array
= sizeof_array
;
150 if (sizeof_array
> 0)
151 memcpy (new_array
, array
, sizeof_array
);
156 /* new property - create it */
157 hw_add_property (me
, property
, type
,
158 NULL
, 0, array
, sizeof_array
,
159 NULL
, temporary_object
);
166 clean_hw_properties (struct hw
*me
)
168 struct hw_property_data
**delete_point
= &me
->properties_of_hw
;
169 while (*delete_point
!= NULL
)
171 struct hw_property_data
*current
= *delete_point
;
172 switch (current
->property
->disposition
)
174 case permanent_object
:
175 /* zap the current value, will be initialized later */
176 ASSERT (current
->init_array
!= NULL
);
177 if (current
->property
->array
!= NULL
)
179 hw_free (me
, (void*)current
->property
->array
);
180 current
->property
->array
= NULL
;
182 delete_point
= &(*delete_point
)->next
;
184 case temporary_object
:
185 /* zap the actual property, was created during simulation run */
186 ASSERT (current
->init_array
== NULL
);
187 *delete_point
= current
->next
;
188 if (current
->property
->array
!= NULL
)
189 hw_free (me
, (void*)current
->property
->array
);
190 hw_free (me
, current
->property
);
191 hw_free (me
, current
);
200 hw_init_static_properties (SIM_DESC sd
,
204 struct hw_property_data
*property
;
205 for (property
= me
->properties_of_hw
;
207 property
= property
->next
)
209 ASSERT (property
->init_array
!= NULL
);
210 ASSERT (property
->property
->array
== NULL
);
211 ASSERT (property
->property
->disposition
== permanent_object
);
212 switch (property
->property
->type
)
215 case boolean_property
:
216 case range_array_property
:
217 case reg_array_property
:
218 case string_property
:
219 case string_array_property
:
220 case integer_property
:
221 /* delete the property, and replace it with the original */
222 hw_set_property (me
, property
->property
->name
,
223 property
->property
->type
,
224 property
->init_array
,
225 property
->sizeof_init_array
);
228 case ihandle_property
:
239 hw_init_runtime_properties (SIM_DESC sd
,
243 struct hw_property_data
*property
;
244 for (property
= me
->properties_of_hw
;
246 property
= property
->next
)
248 switch (property
->property
->disposition
)
250 case permanent_object
:
251 switch (property
->property
->type
)
254 case ihandle_property
:
256 struct hw_instance
*ihandle
;
257 ihandle_runtime_property_spec spec
;
258 ASSERT (property
->init_array
!= NULL
);
259 ASSERT (property
->property
->array
== NULL
);
260 hw_find_ihandle_runtime_property (me
, property
->property
->name
, &spec
);
261 ihandle
= tree_instance (me
, spec
.full_path
);
262 hw_set_ihandle_property (me
, property
->property
->name
, ihandle
);
267 case boolean_property
:
268 case range_array_property
:
269 case integer_property
:
270 case reg_array_property
:
271 case string_property
:
272 case string_array_property
:
273 ASSERT (property
->init_array
!= NULL
);
274 ASSERT (property
->property
->array
!= NULL
);
278 case temporary_object
:
279 ASSERT (property
->init_array
== NULL
);
280 ASSERT (property
->property
->array
!= NULL
);
289 const struct hw_property
*
290 hw_next_property (const struct hw_property
*property
)
292 /* find the property in the list */
293 struct hw
*owner
= property
->owner
;
294 struct hw_property_data
*entry
= owner
->properties_of_hw
;
295 while (entry
!= NULL
&& entry
->property
!= property
)
297 /* now return the following property */
298 ASSERT (entry
!= NULL
); /* must be a member! */
299 if (entry
->next
!= NULL
)
300 return entry
->next
->property
;
306 const struct hw_property
*
307 hw_find_property (struct hw
*me
,
308 const char *property
)
314 else if (property
== NULL
|| strcmp (property
, "") == 0)
316 if (me
->properties_of_hw
== NULL
)
319 return me
->properties_of_hw
->property
;
323 struct hw_property_data
*entry
= find_property_data (me
, property
);
325 return entry
->property
;
332 hw_add_array_property (struct hw
*me
,
333 const char *property
,
337 hw_add_property (me
, property
, array_property
,
338 array
, sizeof_array
, array
, sizeof_array
,
339 NULL
, permanent_object
);
343 hw_set_array_property (struct hw
*me
,
344 const char *property
,
348 hw_set_property (me
, property
, array_property
, array
, sizeof_array
);
351 const struct hw_property
*
352 hw_find_array_property (struct hw
*me
,
353 const char *property
)
355 const struct hw_property
*node
;
356 node
= hw_find_property (me
, property
);
358 hw_abort (me
, "property \"%s\" not found", property
);
359 if (node
->type
!= array_property
)
360 hw_abort (me
, "property \"%s\" of wrong type (array)", property
);
367 hw_add_boolean_property (struct hw
*me
,
368 const char *property
,
371 int32_t new_boolean
= (boolean
? -1 : 0);
372 hw_add_property (me
, property
, boolean_property
,
373 &new_boolean
, sizeof (new_boolean
),
374 &new_boolean
, sizeof (new_boolean
),
375 NULL
, permanent_object
);
379 hw_find_boolean_property (struct hw
*me
,
380 const char *property
)
382 const struct hw_property
*node
;
383 unsigned_cell boolean
;
384 node
= hw_find_property (me
, property
);
386 hw_abort (me
, "property \"%s\" not found", property
);
387 if (node
->type
!= boolean_property
)
388 hw_abort (me
, "property \"%s\" of wrong type (boolean)", property
);
389 ASSERT (sizeof (boolean
) == node
->sizeof_array
);
390 memcpy (&boolean
, node
->array
, sizeof (boolean
));
398 hw_add_ihandle_runtime_property (struct hw
*me
,
399 const char *property
,
400 const ihandle_runtime_property_spec
*ihandle
)
402 /* enter the full path as the init array */
403 hw_add_property (me
, property
, ihandle_property
,
404 ihandle
->full_path
, strlen (ihandle
->full_path
) + 1,
406 NULL
, permanent_object
);
412 hw_find_ihandle_runtime_property (struct hw
*me
,
413 const char *property
,
414 ihandle_runtime_property_spec
*ihandle
)
416 struct hw_property_data
*entry
= find_property_data (me
, property
);
418 hw_abort (me
, "property \"%s\" not found", property
);
419 if (entry
->property
->type
!= ihandle_property
420 || entry
->property
->disposition
!= permanent_object
)
421 hw_abort (me
, "property \"%s\" of wrong type", property
);
422 ASSERT (entry
->init_array
!= NULL
);
424 ihandle
->full_path
= entry
->init_array
;
432 hw_set_ihandle_property (struct hw
*me
,
433 const char *property
,
434 hw_instance
*ihandle
)
437 cells
= H2BE_cell (hw_instance_to_external (ihandle
));
438 hw_set_property (me
, property
, ihandle_property
,
439 &cells
, sizeof (cells
));
446 hw_find_ihandle_property (struct hw
*me
,
447 const char *property
)
449 const hw_property_data
*node
;
450 unsigned_cell ihandle
;
451 hw_instance
*instance
;
453 node
= hw_find_property (me
, property
);
455 hw_abort (me
, "property \"%s\" not found", property
);
456 if (node
->type
!= ihandle_property
)
457 hw_abort (me
, "property \"%s\" of wrong type (ihandle)", property
);
458 if (node
->array
== NULL
)
459 hw_abort (me
, "runtime property \"%s\" not yet initialized", property
);
461 ASSERT (sizeof (ihandle
) == node
->sizeof_array
);
462 memcpy (&ihandle
, node
->array
, sizeof (ihandle
));
463 instance
= external_to_hw_instance (me
, BE2H_cell (ihandle
));
464 ASSERT (instance
!= NULL
);
471 hw_add_integer_property (struct hw
*me
,
472 const char *property
,
476 hw_add_property (me
, property
, integer_property
,
477 &integer
, sizeof (integer
),
478 &integer
, sizeof (integer
),
479 NULL
, permanent_object
);
483 hw_find_integer_property (struct hw
*me
,
484 const char *property
)
486 const struct hw_property
*node
;
488 node
= hw_find_property (me
, property
);
490 hw_abort (me
, "property \"%s\" not found", property
);
491 if (node
->type
!= integer_property
)
492 hw_abort (me
, "property \"%s\" of wrong type (integer)", property
);
493 ASSERT (sizeof (integer
) == node
->sizeof_array
);
494 memcpy (&integer
, node
->array
, sizeof (integer
));
495 return BE2H_cell (integer
);
499 hw_find_integer_array_property (struct hw
*me
,
500 const char *property
,
502 signed_cell
*integer
)
504 const struct hw_property
*node
;
505 int sizeof_integer
= sizeof (*integer
);
508 /* check things sane */
509 node
= hw_find_property (me
, property
);
511 hw_abort (me
, "property \"%s\" not found", property
);
512 if (node
->type
!= integer_property
513 && node
->type
!= array_property
)
514 hw_abort (me
, "property \"%s\" of wrong type (integer or array)", property
);
515 if ((node
->sizeof_array
% sizeof_integer
) != 0)
516 hw_abort (me
, "property \"%s\" contains an incomplete number of cells", property
);
517 if (node
->sizeof_array
<= sizeof_integer
* index
)
520 /* Find and convert the value */
521 cell
= ((signed_cell
*)node
->array
) + index
;
522 *integer
= BE2H_cell (*cell
);
524 return node
->sizeof_array
/ sizeof_integer
;
528 static unsigned_cell
*
529 unit_address_to_cells (const hw_unit
*unit
,
534 ASSERT (nr_cells
== unit
->nr_cells
);
535 for (i
= 0; i
< unit
->nr_cells
; i
++)
537 *cell
= H2BE_cell (unit
->cells
[i
]);
544 static const unsigned_cell
*
545 cells_to_unit_address (const unsigned_cell
*cell
,
550 memset (unit
, 0, sizeof (*unit
));
551 unit
->nr_cells
= nr_cells
;
552 for (i
= 0; i
< unit
->nr_cells
; i
++)
554 unit
->cells
[i
] = BE2H_cell (*cell
);
562 nr_range_property_cells (struct hw
*me
,
565 return ((hw_unit_nr_address_cells (me
)
566 + hw_unit_nr_address_cells (hw_parent (me
))
567 + hw_unit_nr_size_cells (me
))
572 hw_add_range_array_property (struct hw
*me
,
573 const char *property
,
574 const range_property_spec
*ranges
,
577 unsigned sizeof_cells
= (nr_range_property_cells (me
, nr_ranges
)
578 * sizeof (unsigned_cell
));
579 unsigned_cell
*cells
= hw_zalloc (me
, sizeof_cells
);
583 /* copy the property elements over */
585 for (i
= 0; i
< nr_ranges
; i
++)
587 const range_property_spec
*range
= &ranges
[i
];
588 /* copy the child address */
589 cell
= unit_address_to_cells (&range
->child_address
, cell
,
590 hw_unit_nr_address_cells (me
));
591 /* copy the parent address */
592 cell
= unit_address_to_cells (&range
->parent_address
, cell
,
593 hw_unit_nr_address_cells (hw_parent (me
)));
595 cell
= unit_address_to_cells (&range
->size
, cell
,
596 hw_unit_nr_size_cells (me
));
598 ASSERT (cell
== &cells
[nr_range_property_cells (me
, nr_ranges
)]);
601 hw_add_property (me
, property
, range_array_property
,
604 NULL
, permanent_object
);
610 hw_find_range_array_property (struct hw
*me
,
611 const char *property
,
613 range_property_spec
*range
)
615 const struct hw_property
*node
;
616 unsigned sizeof_entry
= (nr_range_property_cells (me
, 1)
617 * sizeof (unsigned_cell
));
618 const unsigned_cell
*cells
;
620 /* locate the property */
621 node
= hw_find_property (me
, property
);
623 hw_abort (me
, "property \"%s\" not found", property
);
624 if (node
->type
!= range_array_property
)
625 hw_abort (me
, "property \"%s\" of wrong type (range array)", property
);
628 if ((node
->sizeof_array
% sizeof_entry
) != 0)
629 hw_abort (me
, "property \"%s\" contains an incomplete number of entries",
633 if (node
->sizeof_array
< sizeof_entry
* (index
+ 1))
636 /* find the range of interest */
637 cells
= (unsigned_cell
*)((char*)node
->array
+ sizeof_entry
* index
);
639 /* copy the child address out - converting as we go */
640 cells
= cells_to_unit_address (cells
, &range
->child_address
,
641 hw_unit_nr_address_cells (me
));
643 /* copy the parent address out - converting as we go */
644 cells
= cells_to_unit_address (cells
, &range
->parent_address
,
645 hw_unit_nr_address_cells (hw_parent (me
)));
647 /* copy the size - converting as we go */
648 cells
= cells_to_unit_address (cells
, &range
->size
,
649 hw_unit_nr_size_cells (me
));
651 return node
->sizeof_array
/ sizeof_entry
;
656 nr_reg_property_cells (struct hw
*me
,
659 return (hw_unit_nr_address_cells (hw_parent (me
))
660 + hw_unit_nr_size_cells (hw_parent (me
))
665 hw_add_reg_array_property (struct hw
*me
,
666 const char *property
,
667 const reg_property_spec
*regs
,
670 unsigned sizeof_cells
= (nr_reg_property_cells (me
, nr_regs
)
671 * sizeof (unsigned_cell
));
672 unsigned_cell
*cells
= hw_zalloc (me
, sizeof_cells
);
676 /* copy the property elements over */
678 for (i
= 0; i
< nr_regs
; i
++)
680 const reg_property_spec
*reg
= ®s
[i
];
681 /* copy the address */
682 cell
= unit_address_to_cells (®
->address
, cell
,
683 hw_unit_nr_address_cells (hw_parent (me
)));
685 cell
= unit_address_to_cells (®
->size
, cell
,
686 hw_unit_nr_size_cells (hw_parent (me
)));
688 ASSERT (cell
== &cells
[nr_reg_property_cells (me
, nr_regs
)]);
691 hw_add_property (me
, property
, reg_array_property
,
694 NULL
, permanent_object
);
700 hw_find_reg_array_property (struct hw
*me
,
701 const char *property
,
703 reg_property_spec
*reg
)
705 const struct hw_property
*node
;
706 unsigned sizeof_entry
= (nr_reg_property_cells (me
, 1)
707 * sizeof (unsigned_cell
));
708 const unsigned_cell
*cells
;
710 /* locate the property */
711 node
= hw_find_property (me
, property
);
713 hw_abort (me
, "property \"%s\" not found", property
);
714 if (node
->type
!= reg_array_property
)
715 hw_abort (me
, "property \"%s\" of wrong type (reg array)", property
);
718 if ((node
->sizeof_array
% sizeof_entry
) != 0)
719 hw_abort (me
, "property \"%s\" contains an incomplete number of entries",
723 if (node
->sizeof_array
< sizeof_entry
* (index
+ 1))
726 /* find the range of interest */
727 cells
= (unsigned_cell
*)((char*)node
->array
+ sizeof_entry
* index
);
729 /* copy the address out - converting as we go */
730 cells
= cells_to_unit_address (cells
, ®
->address
,
731 hw_unit_nr_address_cells (hw_parent (me
)));
733 /* copy the size out - converting as we go */
734 cells
= cells_to_unit_address (cells
, ®
->size
,
735 hw_unit_nr_size_cells (hw_parent (me
)));
737 return node
->sizeof_array
/ sizeof_entry
;
742 hw_add_string_property (struct hw
*me
,
743 const char *property
,
746 hw_add_property (me
, property
, string_property
,
747 string
, strlen (string
) + 1,
748 string
, strlen (string
) + 1,
749 NULL
, permanent_object
);
753 hw_find_string_property (struct hw
*me
,
754 const char *property
)
756 const struct hw_property
*node
;
758 node
= hw_find_property (me
, property
);
760 hw_abort (me
, "property \"%s\" not found", property
);
761 if (node
->type
!= string_property
)
762 hw_abort (me
, "property \"%s\" of wrong type (string)", property
);
763 string
= node
->array
;
764 ASSERT (strlen (string
) + 1 == node
->sizeof_array
);
769 hw_add_string_array_property (struct hw
*me
,
770 const char *property
,
771 const string_property_spec
*strings
,
779 hw_abort (me
, "property \"%s\" must be non-null", property
);
780 /* total up the size of the needed array */
781 for (sizeof_array
= 0, string_nr
= 0;
782 string_nr
< nr_strings
;
785 sizeof_array
+= strlen (strings
[string_nr
]) + 1;
787 /* create the array */
788 array
= (char*) hw_zalloc (me
, sizeof_array
);
791 string_nr
< nr_strings
;
794 strcpy (chp
, strings
[string_nr
]);
795 chp
+= strlen (chp
) + 1;
797 ASSERT (chp
== array
+ sizeof_array
);
799 hw_add_property (me
, property
, string_array_property
,
802 NULL
, permanent_object
);
806 hw_find_string_array_property (struct hw
*me
,
807 const char *property
,
809 string_property_spec
*string
)
811 const struct hw_property
*node
;
812 node
= hw_find_property (me
, property
);
814 hw_abort (me
, "property \"%s\" not found", property
);
818 hw_abort (me
, "property \"%s\" of wrong type", property
);
820 case string_property
:
823 *string
= node
->array
;
824 ASSERT (strlen (*string
) + 1 == node
->sizeof_array
);
829 if (node
->sizeof_array
== 0
830 || ((char*)node
->array
)[node
->sizeof_array
- 1] != '\0')
831 hw_abort (me
, "property \"%s\" invalid for string array", property
);
832 ATTRIBUTE_FALLTHROUGH
;
833 case string_array_property
:
834 ASSERT (node
->sizeof_array
> 0);
835 ASSERT (((char*)node
->array
)[node
->sizeof_array
- 1] == '\0');
837 const char *chp
= node
->array
;
839 /* count the number of strings, keeping an eye out for the one
849 if (nr_entries
== index
)
856 } while (chp
< (char*)node
->array
+ node
->sizeof_array
);
857 if (index
< nr_entries
)
871 hw_add_duplicate_property (struct hw
*me
,
872 const char *property
,
873 const struct hw_property
*original
)
875 struct hw_property_data
*master
;
876 if (original
->disposition
!= permanent_object
)
877 hw_abort (me
, "Can only duplicate permanent objects");
878 /* find the original's master */
879 master
= original
->owner
->properties_of_hw
;
880 while (master
->property
!= original
)
882 master
= master
->next
;
883 ASSERT (master
!= NULL
);
885 /* now duplicate it */
886 hw_add_property (me
, property
,
888 master
->init_array
, master
->sizeof_init_array
,
889 original
->array
, original
->sizeof_array
,
890 original
, permanent_object
);