1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002, 2007, 2008, 2009, 2010, 2011 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/>. */
26 #include "sim-assert.h"
36 /* property entries */
38 struct hw_property_data
{
39 struct hw_property_data
*next
;
40 struct hw_property
*property
;
41 const void *init_array
;
42 unsigned sizeof_init_array
;
46 create_hw_property_data (struct hw
*me
)
51 delete_hw_property_data (struct hw
*me
)
56 /* Device Properties: */
58 static struct hw_property_data
*
59 find_property_data (struct hw
*me
,
62 struct hw_property_data
*entry
;
63 ASSERT (property
!= NULL
);
64 entry
= me
->properties_of_hw
;
67 if (strcmp (entry
->property
->name
, property
) == 0)
76 hw_add_property (struct hw
*me
,
78 hw_property_type type
,
79 const void *init_array
,
80 unsigned sizeof_init_array
,
82 unsigned sizeof_array
,
83 const struct hw_property
*original
,
84 object_disposition disposition
)
86 struct hw_property_data
*new_entry
= NULL
;
87 struct hw_property
*new_value
= NULL
;
89 /* find the list end */
90 struct hw_property_data
**insertion_point
= &me
->properties_of_hw
;
91 while (*insertion_point
!= NULL
)
93 if (strcmp ((*insertion_point
)->property
->name
, property
) == 0)
95 insertion_point
= &(*insertion_point
)->next
;
98 /* create a new value */
99 new_value
= HW_ZALLOC (me
, struct hw_property
);
100 new_value
->name
= (char *) strdup (property
);
101 new_value
->type
= type
;
102 if (sizeof_array
> 0)
104 void *new_array
= hw_zalloc (me
, sizeof_array
);
105 memcpy (new_array
, array
, sizeof_array
);
106 new_value
->array
= new_array
;
107 new_value
->sizeof_array
= sizeof_array
;
109 new_value
->owner
= me
;
110 new_value
->original
= original
;
111 new_value
->disposition
= disposition
;
113 /* insert the value into the list */
114 new_entry
= HW_ZALLOC (me
, struct hw_property_data
);
115 *insertion_point
= new_entry
;
116 if (sizeof_init_array
> 0)
118 void *new_init_array
= hw_zalloc (me
, sizeof_init_array
);
119 memcpy (new_init_array
, init_array
, sizeof_init_array
);
120 new_entry
->init_array
= new_init_array
;
121 new_entry
->sizeof_init_array
= sizeof_init_array
;
123 new_entry
->property
= new_value
;
128 hw_set_property (struct hw
*me
,
129 const char *property
,
130 hw_property_type type
,
134 /* find the property */
135 struct hw_property_data
*entry
= find_property_data (me
, property
);
138 /* existing property - update it */
140 struct hw_property
*value
= entry
->property
;
141 /* check the type matches */
142 if (value
->type
!= type
)
143 hw_abort (me
, "conflict between type of new and old value for property %s", property
);
144 /* replace its value */
145 if (value
->array
!= NULL
)
146 hw_free (me
, (void*)value
->array
);
147 new_array
= (sizeof_array
> 0
148 ? hw_zalloc (me
, sizeof_array
)
150 value
->array
= new_array
;
151 value
->sizeof_array
= sizeof_array
;
152 if (sizeof_array
> 0)
153 memcpy (new_array
, array
, sizeof_array
);
158 /* new property - create it */
159 hw_add_property (me
, property
, type
,
160 NULL
, 0, array
, sizeof_array
,
161 NULL
, temporary_object
);
168 clean_hw_properties (struct hw
*me
)
170 struct hw_property_data
**delete_point
= &me
->properties_of_hw
;
171 while (*delete_point
!= NULL
)
173 struct hw_property_data
*current
= *delete_point
;
174 switch (current
->property
->disposition
)
176 case permenant_object
:
177 /* zap the current value, will be initialized later */
178 ASSERT (current
->init_array
!= NULL
);
179 if (current
->property
->array
!= NULL
)
181 hw_free (me
, (void*)current
->property
->array
);
182 current
->property
->array
= NULL
;
184 delete_point
= &(*delete_point
)->next
;
186 case temporary_object
:
187 /* zap the actual property, was created during simulation run */
188 ASSERT (current
->init_array
== NULL
);
189 *delete_point
= current
->next
;
190 if (current
->property
->array
!= NULL
)
191 hw_free (me
, (void*)current
->property
->array
);
192 hw_free (me
, current
->property
);
193 hw_free (me
, current
);
202 hw_init_static_properties (SIM_DESC sd
,
206 struct hw_property_data
*property
;
207 for (property
= me
->properties_of_hw
;
209 property
= property
->next
)
211 ASSERT (property
->init_array
!= NULL
);
212 ASSERT (property
->property
->array
== NULL
);
213 ASSERT(property
->property
->disposition
== permenant_object
);
214 switch (property
->property
->type
)
217 case boolean_property
:
218 case range_array_property
:
219 case reg_array_property
:
220 case string_property
:
221 case string_array_property
:
222 case integer_property
:
223 /* delete the property, and replace it with the original */
224 hw_set_property (me
, property
->property
->name
,
225 property
->property
->type
,
226 property
->init_array
,
227 property
->sizeof_init_array
);
230 case ihandle_property
:
241 hw_init_runtime_properties (SIM_DESC sd
,
245 struct hw_property_data
*property
;
246 for (property
= me
->properties_of_hw
;
248 property
= property
->next
)
250 switch (property
->property
->disposition
)
252 case permenant_object
:
253 switch (property
->property
->type
)
256 case ihandle_property
:
258 struct hw_instance
*ihandle
;
259 ihandle_runtime_property_spec spec
;
260 ASSERT (property
->init_array
!= NULL
);
261 ASSERT (property
->property
->array
== NULL
);
262 hw_find_ihandle_runtime_property (me
, property
->property
->name
, &spec
);
263 ihandle
= tree_instance (me
, spec
.full_path
);
264 hw_set_ihandle_property (me
, property
->property
->name
, ihandle
);
269 case boolean_property
:
270 case range_array_property
:
271 case integer_property
:
272 case reg_array_property
:
273 case string_property
:
274 case string_array_property
:
275 ASSERT (property
->init_array
!= NULL
);
276 ASSERT (property
->property
->array
!= NULL
);
280 case temporary_object
:
281 ASSERT (property
->init_array
== NULL
);
282 ASSERT (property
->property
->array
!= NULL
);
291 const struct hw_property
*
292 hw_next_property (const struct hw_property
*property
)
294 /* find the property in the list */
295 struct hw
*owner
= property
->owner
;
296 struct hw_property_data
*entry
= owner
->properties_of_hw
;
297 while (entry
!= NULL
&& entry
->property
!= property
)
299 /* now return the following property */
300 ASSERT (entry
!= NULL
); /* must be a member! */
301 if (entry
->next
!= NULL
)
302 return entry
->next
->property
;
308 const struct hw_property
*
309 hw_find_property (struct hw
*me
,
310 const char *property
)
316 else if (property
== NULL
|| strcmp (property
, "") == 0)
318 if (me
->properties_of_hw
== NULL
)
321 return me
->properties_of_hw
->property
;
325 struct hw_property_data
*entry
= find_property_data (me
, property
);
327 return entry
->property
;
334 hw_add_array_property (struct hw
*me
,
335 const char *property
,
339 hw_add_property (me
, property
, array_property
,
340 array
, sizeof_array
, array
, sizeof_array
,
341 NULL
, permenant_object
);
345 hw_set_array_property (struct hw
*me
,
346 const char *property
,
350 hw_set_property (me
, property
, array_property
, array
, sizeof_array
);
353 const struct hw_property
*
354 hw_find_array_property (struct hw
*me
,
355 const char *property
)
357 const struct hw_property
*node
;
358 node
= hw_find_property (me
, property
);
360 hw_abort (me
, "property \"%s\" not found", property
);
361 if (node
->type
!= array_property
)
362 hw_abort (me
, "property \"%s\" of wrong type (array)", property
);
369 hw_add_boolean_property (struct hw
*me
,
370 const char *property
,
373 signed32 new_boolean
= (boolean
? -1 : 0);
374 hw_add_property (me
, property
, boolean_property
,
375 &new_boolean
, sizeof(new_boolean
),
376 &new_boolean
, sizeof(new_boolean
),
377 NULL
, permenant_object
);
381 hw_find_boolean_property (struct hw
*me
,
382 const char *property
)
384 const struct hw_property
*node
;
385 unsigned_cell boolean
;
386 node
= hw_find_property (me
, property
);
388 hw_abort (me
, "property \"%s\" not found", property
);
389 if (node
->type
!= boolean_property
)
390 hw_abort (me
, "property \"%s\" of wrong type (boolean)", property
);
391 ASSERT (sizeof (boolean
) == node
->sizeof_array
);
392 memcpy (&boolean
, node
->array
, sizeof (boolean
));
400 hw_add_ihandle_runtime_property (struct hw
*me
,
401 const char *property
,
402 const ihandle_runtime_property_spec
*ihandle
)
404 /* enter the full path as the init array */
405 hw_add_property (me
, property
, ihandle_property
,
406 ihandle
->full_path
, strlen(ihandle
->full_path
) + 1,
408 NULL
, permenant_object
);
414 hw_find_ihandle_runtime_property (struct hw
*me
,
415 const char *property
,
416 ihandle_runtime_property_spec
*ihandle
)
418 struct hw_property_data
*entry
= find_property_data (me
, property
);
419 HW_TRACE ((me
, "hw_find_ihandle_runtime_property(property=%s)\n", property
));
421 hw_abort (me
, "property \"%s\" not found", property
);
422 if (entry
->property
->type
!= ihandle_property
423 || entry
->property
->disposition
!= permenant_object
)
424 hw_abort (me
, "property \"%s\" of wrong type", property
);
425 ASSERT (entry
->init_array
!= NULL
);
427 ihandle
->full_path
= entry
->init_array
;
435 hw_set_ihandle_property (struct hw
*me
,
436 const char *property
,
437 hw_instance
*ihandle
)
440 cells
= H2BE_cell (hw_instance_to_external (ihandle
));
441 hw_set_property (me
, property
, ihandle_property
,
442 &cells
, sizeof (cells
));
449 hw_find_ihandle_property (struct hw
*me
,
450 const char *property
)
452 const hw_property_data
*node
;
453 unsigned_cell ihandle
;
454 hw_instance
*instance
;
456 node
= hw_find_property (me
, property
);
458 hw_abort (me
, "property \"%s\" not found", property
);
459 if (node
->type
!= ihandle_property
)
460 hw_abort(me
, "property \"%s\" of wrong type (ihandle)", property
);
461 if (node
->array
== NULL
)
462 hw_abort(me
, "runtime property \"%s\" not yet initialized", property
);
464 ASSERT (sizeof(ihandle
) == node
->sizeof_array
);
465 memcpy (&ihandle
, node
->array
, sizeof(ihandle
));
466 instance
= external_to_hw_instance (me
, BE2H_cell(ihandle
));
467 ASSERT (instance
!= NULL
);
474 hw_add_integer_property (struct hw
*me
,
475 const char *property
,
479 hw_add_property (me
, property
, integer_property
,
480 &integer
, sizeof(integer
),
481 &integer
, sizeof(integer
),
482 NULL
, permenant_object
);
486 hw_find_integer_property (struct hw
*me
,
487 const char *property
)
489 const struct hw_property
*node
;
491 HW_TRACE ((me
, "hw_find_integer(property=%s)\n", property
));
492 node
= hw_find_property (me
, property
);
494 hw_abort (me
, "property \"%s\" not found", property
);
495 if (node
->type
!= integer_property
)
496 hw_abort (me
, "property \"%s\" of wrong type (integer)", property
);
497 ASSERT (sizeof(integer
) == node
->sizeof_array
);
498 memcpy (&integer
, node
->array
, sizeof (integer
));
499 return BE2H_cell (integer
);
503 hw_find_integer_array_property (struct hw
*me
,
504 const char *property
,
506 signed_cell
*integer
)
508 const struct hw_property
*node
;
509 int sizeof_integer
= sizeof (*integer
);
511 HW_TRACE ((me
, "hw_find_integer(property=%s)\n", property
));
513 /* check things sane */
514 node
= hw_find_property (me
, property
);
516 hw_abort (me
, "property \"%s\" not found", property
);
517 if (node
->type
!= integer_property
518 && node
->type
!= array_property
)
519 hw_abort (me
, "property \"%s\" of wrong type (integer or array)", property
);
520 if ((node
->sizeof_array
% sizeof_integer
) != 0)
521 hw_abort (me
, "property \"%s\" contains an incomplete number of cells", property
);
522 if (node
->sizeof_array
<= sizeof_integer
* index
)
525 /* Find and convert the value */
526 cell
= ((signed_cell
*)node
->array
) + index
;
527 *integer
= BE2H_cell (*cell
);
529 return node
->sizeof_array
/ sizeof_integer
;
533 static unsigned_cell
*
534 unit_address_to_cells (const hw_unit
*unit
,
539 ASSERT(nr_cells
== unit
->nr_cells
);
540 for (i
= 0; i
< unit
->nr_cells
; i
++)
542 *cell
= H2BE_cell (unit
->cells
[i
]);
549 static const unsigned_cell
*
550 cells_to_unit_address (const unsigned_cell
*cell
,
555 memset(unit
, 0, sizeof(*unit
));
556 unit
->nr_cells
= nr_cells
;
557 for (i
= 0; i
< unit
->nr_cells
; i
++)
559 unit
->cells
[i
] = BE2H_cell (*cell
);
567 nr_range_property_cells (struct hw
*me
,
570 return ((hw_unit_nr_address_cells (me
)
571 + hw_unit_nr_address_cells (hw_parent (me
))
572 + hw_unit_nr_size_cells (me
))
577 hw_add_range_array_property (struct hw
*me
,
578 const char *property
,
579 const range_property_spec
*ranges
,
582 unsigned sizeof_cells
= (nr_range_property_cells (me
, nr_ranges
)
583 * sizeof (unsigned_cell
));
584 unsigned_cell
*cells
= hw_zalloc (me
, sizeof_cells
);
588 /* copy the property elements over */
590 for (i
= 0; i
< nr_ranges
; i
++)
592 const range_property_spec
*range
= &ranges
[i
];
593 /* copy the child address */
594 cell
= unit_address_to_cells (&range
->child_address
, cell
,
595 hw_unit_nr_address_cells (me
));
596 /* copy the parent address */
597 cell
= unit_address_to_cells (&range
->parent_address
, cell
,
598 hw_unit_nr_address_cells (hw_parent (me
)));
600 cell
= unit_address_to_cells (&range
->size
, cell
,
601 hw_unit_nr_size_cells (me
));
603 ASSERT (cell
== &cells
[nr_range_property_cells (me
, nr_ranges
)]);
606 hw_add_property (me
, property
, range_array_property
,
609 NULL
, permenant_object
);
615 hw_find_range_array_property (struct hw
*me
,
616 const char *property
,
618 range_property_spec
*range
)
620 const struct hw_property
*node
;
621 unsigned sizeof_entry
= (nr_range_property_cells (me
, 1)
622 * sizeof (unsigned_cell
));
623 const unsigned_cell
*cells
;
625 /* locate the property */
626 node
= hw_find_property (me
, property
);
628 hw_abort (me
, "property \"%s\" not found", property
);
629 if (node
->type
!= range_array_property
)
630 hw_abort (me
, "property \"%s\" of wrong type (range array)", property
);
633 if ((node
->sizeof_array
% sizeof_entry
) != 0)
634 hw_abort (me
, "property \"%s\" contains an incomplete number of entries",
638 if (node
->sizeof_array
< sizeof_entry
* (index
+ 1))
641 /* find the range of interest */
642 cells
= (unsigned_cell
*)((char*)node
->array
+ sizeof_entry
* index
);
644 /* copy the child address out - converting as we go */
645 cells
= cells_to_unit_address (cells
, &range
->child_address
,
646 hw_unit_nr_address_cells (me
));
648 /* copy the parent address out - converting as we go */
649 cells
= cells_to_unit_address (cells
, &range
->parent_address
,
650 hw_unit_nr_address_cells (hw_parent (me
)));
652 /* copy the size - converting as we go */
653 cells
= cells_to_unit_address (cells
, &range
->size
,
654 hw_unit_nr_size_cells (me
));
656 return node
->sizeof_array
/ sizeof_entry
;
661 nr_reg_property_cells (struct hw
*me
,
664 return (hw_unit_nr_address_cells (hw_parent(me
))
665 + hw_unit_nr_size_cells (hw_parent(me
))
670 hw_add_reg_array_property (struct hw
*me
,
671 const char *property
,
672 const reg_property_spec
*regs
,
675 unsigned sizeof_cells
= (nr_reg_property_cells (me
, nr_regs
)
676 * sizeof (unsigned_cell
));
677 unsigned_cell
*cells
= hw_zalloc (me
, sizeof_cells
);
681 /* copy the property elements over */
683 for (i
= 0; i
< nr_regs
; i
++)
685 const reg_property_spec
*reg
= ®s
[i
];
686 /* copy the address */
687 cell
= unit_address_to_cells (®
->address
, cell
,
688 hw_unit_nr_address_cells (hw_parent (me
)));
690 cell
= unit_address_to_cells (®
->size
, cell
,
691 hw_unit_nr_size_cells (hw_parent (me
)));
693 ASSERT (cell
== &cells
[nr_reg_property_cells (me
, nr_regs
)]);
696 hw_add_property (me
, property
, reg_array_property
,
699 NULL
, permenant_object
);
705 hw_find_reg_array_property (struct hw
*me
,
706 const char *property
,
708 reg_property_spec
*reg
)
710 const struct hw_property
*node
;
711 unsigned sizeof_entry
= (nr_reg_property_cells (me
, 1)
712 * sizeof (unsigned_cell
));
713 const unsigned_cell
*cells
;
715 /* locate the property */
716 node
= hw_find_property (me
, property
);
718 hw_abort (me
, "property \"%s\" not found", property
);
719 if (node
->type
!= reg_array_property
)
720 hw_abort (me
, "property \"%s\" of wrong type (reg array)", property
);
723 if ((node
->sizeof_array
% sizeof_entry
) != 0)
724 hw_abort (me
, "property \"%s\" contains an incomplete number of entries",
728 if (node
->sizeof_array
< sizeof_entry
* (index
+ 1))
731 /* find the range of interest */
732 cells
= (unsigned_cell
*)((char*)node
->array
+ sizeof_entry
* index
);
734 /* copy the address out - converting as we go */
735 cells
= cells_to_unit_address (cells
, ®
->address
,
736 hw_unit_nr_address_cells (hw_parent (me
)));
738 /* copy the size out - converting as we go */
739 cells
= cells_to_unit_address (cells
, ®
->size
,
740 hw_unit_nr_size_cells (hw_parent (me
)));
742 return node
->sizeof_array
/ sizeof_entry
;
747 hw_add_string_property (struct hw
*me
,
748 const char *property
,
751 hw_add_property (me
, property
, string_property
,
752 string
, strlen(string
) + 1,
753 string
, strlen(string
) + 1,
754 NULL
, permenant_object
);
758 hw_find_string_property (struct hw
*me
,
759 const char *property
)
761 const struct hw_property
*node
;
763 node
= hw_find_property (me
, property
);
765 hw_abort (me
, "property \"%s\" not found", property
);
766 if (node
->type
!= string_property
)
767 hw_abort (me
, "property \"%s\" of wrong type (string)", property
);
768 string
= node
->array
;
769 ASSERT (strlen(string
) + 1 == node
->sizeof_array
);
774 hw_add_string_array_property (struct hw
*me
,
775 const char *property
,
776 const string_property_spec
*strings
,
784 hw_abort (me
, "property \"%s\" must be non-null", property
);
785 /* total up the size of the needed array */
786 for (sizeof_array
= 0, string_nr
= 0;
787 string_nr
< nr_strings
;
790 sizeof_array
+= strlen (strings
[string_nr
]) + 1;
792 /* create the array */
793 array
= (char*) hw_zalloc (me
, sizeof_array
);
796 string_nr
< nr_strings
;
799 strcpy (chp
, strings
[string_nr
]);
800 chp
+= strlen (chp
) + 1;
802 ASSERT (chp
== array
+ sizeof_array
);
804 hw_add_property (me
, property
, string_array_property
,
807 NULL
, permenant_object
);
811 hw_find_string_array_property (struct hw
*me
,
812 const char *property
,
814 string_property_spec
*string
)
816 const struct hw_property
*node
;
817 node
= hw_find_property (me
, property
);
819 hw_abort (me
, "property \"%s\" not found", property
);
823 hw_abort (me
, "property \"%s\" of wrong type", property
);
825 case string_property
:
828 *string
= node
->array
;
829 ASSERT (strlen(*string
) + 1 == node
->sizeof_array
);
834 if (node
->sizeof_array
== 0
835 || ((char*)node
->array
)[node
->sizeof_array
- 1] != '\0')
836 hw_abort (me
, "property \"%s\" invalid for string array", property
);
838 case string_array_property
:
839 ASSERT (node
->sizeof_array
> 0);
840 ASSERT (((char*)node
->array
)[node
->sizeof_array
- 1] == '\0');
842 const char *chp
= node
->array
;
844 /* count the number of strings, keeping an eye out for the one
854 if (nr_entries
== index
)
861 } while (chp
< (char*)node
->array
+ node
->sizeof_array
);
862 if (index
< nr_entries
)
876 hw_add_duplicate_property (struct hw
*me
,
877 const char *property
,
878 const struct hw_property
*original
)
880 struct hw_property_data
*master
;
881 HW_TRACE ((me
, "hw_add_duplicate_property(property=%s, ...)\n", property
));
882 if (original
->disposition
!= permenant_object
)
883 hw_abort (me
, "Can only duplicate permenant objects");
884 /* find the original's master */
885 master
= original
->owner
->properties_of_hw
;
886 while (master
->property
!= original
)
888 master
= master
->next
;
889 ASSERT(master
!= NULL
);
891 /* now duplicate it */
892 hw_add_property (me
, property
,
894 master
->init_array
, master
->sizeof_init_array
,
895 original
->array
, original
->sizeof_array
,
896 original
, permenant_object
);